I have displayed the data from the SQL DB in GRID view control using ASP.NET web application. I was able to display the data from the db. In one column I have values which is 1 and 0 in the db. This is displayed as check boxes in the grid view. I want to
change this check box to some "Text Values".
noet that, the datafield should indicate actual name of database column to be mapped to respective column.
then, create RowDataBound event for gridview and add this code in it
public void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
if(((Label)e.Row.FindControl("lblyesNo")).Text == "1")
((Label)e.Row.FindControl("lblyesNo")).Text = "one" //or some other text instad of displaying 1
else
((Label)e.Row.FindControl("lblyesNo")).Text = "zero" //or some other text instad of displaying 1
}
}
hope this helps...
Cheers!
KK
Please mark as Answer if post helps in resolving your issue
My Site
Please try the following code at pageload where you are filled the values in GRID view control.Take a hidden field in griedview where the value 1 or 0 is stored and then compare the value if it is 1 then the text of checkbox is True else it is False.
Maruthu
None
0 Points
4 Posts
Change the Check box displayed in grid view to text
Apr 23, 2012 10:56 AM|LINK
Hi All
I have displayed the data from the SQL DB in GRID view control using ASP.NET web application. I was able to display the data from the db. In one column I have values which is 1 and 0 in the db. This is displayed as check boxes in the grid view. I want to change this check box to some "Text Values".
Any help would be really appreciated.
Maruthu.
http://sharepoint-works.blogspot.com
kedarrkulkar...
All-Star
34535 Points
5554 Posts
Re: Change the Check box displayed in grid view to text
Apr 23, 2012 11:12 AM|LINK
u can set autogeneratecolumn to false and specify each of three columns explicitly... like this
<asp:GridView ID="GridView1" Runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField ReadOnly="True" HeaderText="column1" DataField="DBColumn1"> </asp:BoundField> <asp:BoundField HeaderText="column2" DataField="DBColumn2" > </asp:BoundField> <asp:BoundField HeaderText="column3" DataField="DBColumn3" > </asp:BoundField> <asp:TemplateField HeaderText="column4"> <ItemTemplate> <asp:Label id="lblYesNo" runat="server"/> </ItemTemplate> </asp:TemplateField > <Columns> </asp:GridView>noet that, the datafield should indicate actual name of database column to be mapped to respective column.
then, create RowDataBound event for gridview and add this code in it
public void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if(((Label)e.Row.FindControl("lblyesNo")).Text == "1") ((Label)e.Row.FindControl("lblyesNo")).Text = "one" //or some other text instad of displaying 1 else ((Label)e.Row.FindControl("lblyesNo")).Text = "zero" //or some other text instad of displaying 1 } }hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
poojajoon
Member
228 Points
52 Posts
Re: Change the Check box displayed in grid view to text
Apr 23, 2012 11:51 AM|LINK
Please try the following code at pageload where you are filled the values in GRID view control.Take a hidden field in griedview where the value 1 or 0 is stored and then compare the value if it is 1 then the text of checkbox is True else it is False.
Default.aspx code is
<asp:GridView ID="GridView1" runat="server"
AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-ForeColor="white" AutoGenerateColumns="false"
Font-Names="Arial" Font-Size="11pt" HeaderStyle-BackColor="green" Width="920px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>Value of Checkbox</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Label ID="student_id" Enabled="false" runat="server" Text='<%#Eval("value") %>' Visible="false"></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="Green" ForeColor="White"></HeaderStyle>
<AlternatingRowStyle BackColor="#C2D69B"></AlternatingRowStyle>
</asp:GridView>
Default.aspx.cs code is:
if (!Page.IsPostBack)
{
string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
SqlConnection conn = new SqlConnection(strcon);
conn.Open();
string cmd = "select value from table_ name";
SqlCommand query = new SqlCommand(cmd, conn);
SqlDataAdapter da = new SqlDataAdapter(query);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
foreach (GridViewRow GVRow in GridView1.Rows)
{
if (((Label)GVRow.Cells[0].FindControl("value")).Text == "1")
{
((CheckBox)GVRow.Cells["chk_1"].FindControl("CheckBox1")).Text = "True";
}
else
{
((CheckBox)GVRow.Cells["chk_1"].FindControl("chk_1")).Text = "False";
}
}
conn.Close();
}
Hope it will help you.
ZeeshanAnsar...
Participant
878 Points
264 Posts
Re: Change the Check box displayed in grid view to text
Apr 23, 2012 12:14 PM|LINK
Pooja,
this you have to do in GridView_RowDataBoudn()
with poper code like e.Row.FindControl() method .
Please 'Mark as Answer' if this post helps you.
Maruthu
None
0 Points
4 Posts
Re: Change the Check box displayed in grid view to text
Apr 23, 2012 12:40 PM|LINK
Hi
Thanks for your reply!
I am getting error in this line
if(((Label)e.Row.FindControl("lblyesNo")).Text == 1)
Error 1 Operator '==' cannot be applied to operands of type 'string' and 'int'
Maruthu.
http://sharepoint-works.blogspot.com
poojajoon
Member
228 Points
52 Posts
Re: Change the Check box displayed in grid view to text
Apr 24, 2012 03:58 AM|LINK
ZeeshanAnsari
No you just need to write this code at page load only while you are filling the GRID view control.
poojajoon
Member
228 Points
52 Posts
Re: Change the Check box displayed in grid view to text
Apr 24, 2012 04:01 AM|LINK
Maruthu
you can try
if( Convert.ToInt16(((Label)e.Row.FindControl("lblyesNo")).Text)== 1)
Hope This will help you.
kedarrkulkar...
All-Star
34535 Points
5554 Posts
Re: Change the Check box displayed in grid view to text
Apr 24, 2012 05:08 AM|LINK
which example r u referring to?
did u read my last post?
it should be
if(((Label)e.Row.FindControl("lblyesNo")).Text == "1")
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site