GridView CheckBox - asp.net

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>

Related

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

Hiding a cell with a Hidden Field

In my GridView, I am using a Hidden Field to store some data that is not supposed to be seen by the user:
<Columns>
<asp:BoundField DataField="Название" HeaderText="Название" ItemStyle-Width="250px" HeaderStyle-Width="250px" />
<asp:BoundField DataField="RDName" HeaderText="РД" ItemStyle-Width="250px" HeaderStyle-Width="250px" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="RD_ID" runat="server" Value='<%# Eval("RD_ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
I have a problem with this. Though the data is not seen in the GridView, the additional empty cell is still there. Could you please tell me how I can hide it completely?
Thanks,
David
Can't you do it like this?
<Columns>
<asp:BoundField DataField="Название" HeaderText="Название" ItemStyle-Width="250px" HeaderStyle-Width="250px" />
<asp:TemplateField HeaderText="РД" ItemStyle-Width="250px" HeaderStyle-Width="250px">
<ItemTemplate>
<asp:Label ID="RD_Name" runat="server" Text='<%# Eval("RDName") %>' />
<asp:HiddenField ID="RD_ID" runat="server" Value='<%# Eval("RD_ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can always put the hidden element alongside with any TemplateField ItemTemplate object. You don't have to create a cell to contain the hidden element.

how to bind checkbox value from datatable in gridview

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>

How to get cell contents in ASP.NET GridView while handling OnRowEditing?

I have a GridView with a BoundField column and quite a few item templates like the following:
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="False" OnRowCommand="gvUsers_RowCommand"
OnRowDataBound="gvUsers_RowDataBound" DataKeyNames="UserId" OnRowEditing="gvUsers_OnRowEditing"
OnRowUpdating="gvUsers_OnRowUpdating" OnRowUpdated="gvUsers_OnRowUpdated"
DataSourceID="DataSource1" Width="807px" Height="105px"
AllowPaging="True" >
<Columns>
<asp:BoundField DataField="UserName" HeaderText="User Name"
SortExpression="UserName" />
<asp:TemplateField HeaderText="Approver">
<ItemTemplate>
<asp:CheckBox ID="cbApprover" runat="server" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Auditor">
<ItemTemplate>
<asp:CheckBox ID="cbAuditor" runat="server" Enabled="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Edit" Text="Edit" />
<asp:Label ID="lblPipe1" runat="server" Text=" | " />
<asp:LinkButton ID="btnUpdate" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Update" Text="Update" />
<asp:Label ID="lblPipe" runat="server" Text=" | " />
<asp:LinkButton ID="btnDelete" runat="server"
CommandArgument='<%# Eval("UserName") %>' CommandName="Remove"
OnClientClick="return confirm('Are you sure you want to delete this user?');"
Text="Delete" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
While handling the Edit link button I need to use the value in the BoundField, UserName. Unfortunately during the OnRowEditing handler, all strings are empty "". This is also true in the ensuing OnRowDataBound handler for the row in question where e.Row.RowState == DataControlRowState.Edit. Is there a way to obtain this value after the user clicks on the Edit link, and commences the OnRowEditing event?
I arrived at a solution that was a bit more complicated than I was hoping for. I'll show code later but I did the following:
Created a HiddenField to hold the
value of the cell I wanted to edit in
its view state.
In the OnRowDataBound handler, I assigned the value to the
HiddenField.
The ItemTemplate, as seen above, sends the UserName as an
argument. I store this value in
the HiddenField.
The OnRowEditing is fired after the Command handler. That is where I
read the HiddenField.
If anyone has a simpler solution I would love hear about it.
Could it be that you're using late binding in TemplatedItems without EditItemTemplates?

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") %>' />

Resources