how to bind checkbox value from datatable in gridview - asp.net

I need to figure out how to bind a CheckBox value in a GridView, I have written CheckBox.Checked= DataBinder.Eval(Container.DataItem, "IsSubscribed") in GridView, but the CheckBox is always checked, even when IsSubscribed is false.
I have bound the grid in Page_Load, before the page has posted back. Here is my code:
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox
ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed"
Checked='<%# DataBinder.Eval(Container.DataItem, "IsSubscribed") %>'/>
</ItemTemplate>
</asp:TemplateField>
Thanks.

Put this code as your Item Template element:
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed"
Checked='<%#bool.Parse(Eval("IsSubscribed").ToString())%>' />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox
ID="chkIsSubscribed" runat="server" HeaderText="IsSubscribed"
Checked='<%#Convert.ToBoolean(Eval("IsSubscribed")) %>'/>
</ItemTemplate>
</asp:TemplateField>
please use this......

Eval() give an object type.
So you have to use Eval(..).ToString() if you want to compare it...
Like:
<asp:TemplateField HeaderText="Actif">
<ItemTemplate><asp:CheckBox ID="chkIsACTIF" runat="server" Enabled="false" Checked='<%# (Eval("ACTIF").ToString() == "1" ? true : false) %>' /></ItemTemplate>
<EditItemTemplate><asp:CheckBox ID="chkACTIF" runat="server" Checked='<%# (Eval("ACTIF").ToString() == "1" ? true : false) %>' Enabled="true" /></EditItemTemplate>
<FooterTemplate><asp:CheckBox ID="chkNewACTIF" runat="server" Checked="true" /></FooterTemplate>
</asp:TemplateField>

Related

Label text databind to a column alias with dot

I have this Select statement:
SELECT recordID As [Zap.st.] FROM SomeTable
When I try to bind the result to a GridView with template fields using an asp:Label I get this error:
System.Web.HttpException (0x80004005): DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Zap'
This is a sample GridView:
<asp:GridView ID="gvMainData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Zap.št." SortExpression="Zap.st." ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblZapSt" runat="server" Text='<%# Eval("Zap.st.") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I tried:
Text='<%# Eval("[Zap.st.]") %>'
Text='<%# Eval("'Zap.st.'") %>'
but none of these seem to work.
Edit: To further clarify my problem:
My Select statement is a View on Sql Server, the results in my asp.net come in as [Zap.st.], there is no reference that this is [recordID]
Try this code :
<asp:GridView ID="gvMainData" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Zap.št." SortExpression="Zap.st." ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblZapSt" runat="server" Text='<%# Eval("recordID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I found a similar question on SO, this is the solution:
<asp:Label ID="lblZapStD" runat="server" Text='<%# DataBinder.GetPropertyValue(Container.DataItem, "Zap.st.") %>' />
And this is the original question with an accepted answer.

anyone can tell me how to compare the var

i want to compare this two value on grid view but when i using .text it will occur error?
the client side code look like this
<asp:TemplateField >
<ItemTemplate>
<asp:Label ID="lblremainqty" runat="server" Text='<%# Eval("qtycart") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Error" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblError" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Try use this
int.Parse(Qty.Text) > int.Parse(RemainQty.Text)
or if it is not int value use float or decimal Parse etc. I hope it will help.
PS
Qty.ToString() will return you name of type Label.
Operator >(Greater than) is only used to compare Integer Value Not
String Value. If still you need to use that operation Then you can
convert into Int and Use that operation
You can use :
Convert.ToInt32(Qty.Text) > Convert.ToInt32(RemainQty.Text)
the client side code look like this
<asp:TemplateField HeaderText="Quantity" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblQty" runat="server" Text='<%# Eval("Quantity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:Label ID="lblremainqty" runat="server" Text='<%# Eval("qtycart") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Error" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblError" runat="server" Text=''></asp:Label>
</ItemTemplate>
</asp:TemplateField>

How to Set the Checkbox value inside GridView?

How to set the CheckBox value, which is located inside a Gridview ?
<asp:GridView ID="gviewPermission" runat="server"
onrowdatabound="gviewPermission_RowDataBound"
onrowupdated="gviewPermission_RowUpdated"
onrowupdating="gviewPermission_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Allow" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Check_Allow" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deny" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="Check_Deny" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The check box value has to set based upon some condition....
In the gviewPermission_RowDataBound function do:
if(e.Row.RowType == DataControlRowType.DataRow)
((CheckBox)e.Row.FindControl("Check_Allow")).Checked = SomeCondition;
Or if the condition is coming directly from the datasource you can do:
<ItemTemplate>
<asp:CheckBox ID="Check_Allow" runat="server"
Checked='<%# Eval("ConditionFromDs") %>' />
</ItemTemplate>
If the value of column is boolean then. Try the below code
<ItemTemplate>
<center>
<asp:CheckBox ID="chkSelect" Checked='<%#Convert.ToBoolean(Eval("isChecked"))%>' runat="server"></asp:CheckBox></center>
</ItemTemplate>
Where "isChecked" is the column name.
the CheckBox control has an attribute called Checked that works similarly to the html counterpart attribute. So set this attribute either in the aspx markup:
<asp:CheckBox ID="Check_Allow" runat="server" Checked='<%= someCondition == true %>' />
or in your code-behind.
<ItemTemplate>
<asp:CheckBox runat="server" checked='<%# bool.Parse(Eval("check").ToString()) %>' ID="chkselet" />
</ItemTemplate>
check value must be true or false

Merge datagrid cells

i have to merge the cells from the cell that does not contain the radio button, to the cell that contains the radio button.
here the link for the interface
http://i839.photobucket.com/albums/zz316/girish_kolte/untitled.jpg
You need to use the TemplateField and here is a tutorial that explains some of the other fields that GridView offers as well.
<asp:GridView ID="gvwAirportSchedule" runat="server">
<Columns>
....
<asp:TemplateField>
<ItemTemplate HeaderText="Airport">
<asp:RadioButton ID="rbAirport" runat="server" Visible='<%# (bool)Eval("IsDestination") %>' />
<asp:Label runat="server" ID="Label1" Text='<%# Eval("Airport") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</Columns>
</asp:GridView>
good answer from David.
One could reduce, by omitting Lable like below
<asp:RadioButton ID="RadioButton1" runat="server" Text='<%# Eval("Airport") %>' />

GridView CheckBox

How do I check the checkbox in gridview on the database bit '1' and uncheck on '0' automatically and then submit the checked values into database? please help me.
I tried this and it simply did the job:
<asp:TemplateField HeaderText="Is Active">
<ItemTemplate>
<asp:CheckBox ID="Chk" runat="server" Checked='<%# Bind("fieldname") %>' />
</ItemTemplate>
</asp:TemplateField>
This should get you going
http://www.asp.net/Learn/data-access/tutorial-52-vb.aspx
A templated field may work:
<asp:TemplateField HeaderText="Serial Number" SortExpression="SERIALNUMBER" >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# iif(Bind("FIELD")=0,"False","True") %>' />
</ItemTemplate>
</asp:TemplateField>

Resources