I need to load the Checked Value in the column Status if the Value in the Attendance_Status = "Attended" and uncheck if the value is Unattended.
Below is the screen shot of the form:
I am trying to add the code below but it doesn't work:
foreach (DataGridViewRow row in dataGridView2.Rows) {
if (row.Cells["Attendance_Status"].Value == "Attendance")
{
row.Cells["Status"].Selected = true;
}
else
{
row.Cells["Status"].Selected = false;
}
}
Please help!!
You can try with finding the control in the gridview and then the value of that checked either 0 or 1 based on whether it is selected or not as shown below.
foreach (GridViewRow row in gvGridview.Rows)
{
CheckBox cbAddAction = (CheckBox)row.FindControl("chkAddAction");
int addPermission = Convert.ToInt16(cbAddAction.Checked);
}
You can check the below gridview definition/declaration for more help.
<asp:GridView ID="gvUserPermission" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CssClass="table table-striped table-bordered bootstrap-datatable" HorizontalAlign="Center"
OnRowDataBound="gvUserPermission_RowDataBound" PageSize="50"
DataKeyNames="ModuleId"
onselectedindexchanged="gvUserPermission_SelectedIndexChanged">
<PagerSettings FirstPageText="« First" LastPageText="Last »" Mode="NumericFirstLast"
PageButtonCount="10" />
<Columns>
<asp:BoundField DataField="Modulename" HeaderText="Module Name" ItemStyle-Width="450px"
SortExpression="Modulename" />
<asp:TemplateField HeaderText="Add Action">
<ItemTemplate>
<asp:CheckBox ID="chkAddAction" Checked='<%# Eval("AddAction") == DBNull.Value ? false : Convert.ToBoolean(Eval("AddAction")) %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
To load the data you can use the following method. It will check the value as per the data without any additional loop.
<asp:TemplateField HeaderText="Edit Action">
<ItemTemplate>
<asp:CheckBox ID="chkEditAction" Checked='<%# Eval("EditAction") == DBNull.Value ? false : Convert.ToBoolean(Eval("EditAction")) %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
In your SQL Server datasource write a case statement to return NULL
for values which is not Attended
as given below.
create table Attendace (Id int, Attendance_Status Varchar(50))
insert into Attendace values (1, 'Attended'),
(2, 'Not Attended')
select id
, case Attendance_Status when 'Attended' then 1 else null
end as Attendance_Status
from Attendace
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.