Hello everybody,
I am developing an intranet web application using ASP.NET. Now, I need to develop a training record for my department. The department consists of the four groups. For that, I developed this record as following: I used a Repeator and inside it I put GridView since I have three different types of training courses. And I used a HiddenField to determine the ID for each training course type. For retrieving the data from the database, I used the StoredProcedure. Everything works fine and well after this long task.
Now, I need to set these GridViews for updating by the admin. So since I have
a lot of employees and a lot of courses in each GridView, I think the best
way to update the GridView is to show the blank fields as checkboxes and when
the Admin wants to update the record of one of the employees, all what he
needs to do is just checking the checkboxes and click update button.
I could
be able to show the empty fields as checkboxes but now I don't know how to
post the value of checking the checkbox to the database.
The scenario that I am looking for it is to make the system check every
checkbox in each GridView, if it is already checked before, leave it as it
is. If it was empty and it is just checked now, it should be added to the
database in the record of the employee.
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("GroupID")%>' />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommandType="StoredProcedure" SelectCommand="kbiReport">
<%----%>
<%----%>
<SelectParameters>
<%----%>
<asp:ControlParameter ControlID="HiddenField1" Name="GroupID" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server"
AllowSorting="True"
CellPadding="3"
DataSourceID="SqlDataSource1"
CssClass="mGrid"
AlternatingRowStyle-CssClass="alt"
RowStyle-HorizontalAlign="Center"
OnRowDataBound="GridView1_RowDataBound" OnDataBound="GridView1_DataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<HeaderStyle Font-Bold = "true" ForeColor="Black"/>
<Columns>
<asp:CommandField ShowSelectButton="True" />
<%----%>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT DISTINCT GroupID FROM courses">
</asp:SqlDataSource>
<asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
<br /><br />
The Code-Behind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell c in e.Row.Cells)
{
if (c.Text.Equals("Yes"))
{
c.BackColor = System.Drawing.Color.LightGreen;
}
}
}
else if(e.Row.RowType == DataControlRowType.Header){
switch (((HiddenField)((GridView)sender).Parent.FindControl("HiddenField1")).Value)
{
case "1":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightBlue;
break;
case "2":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.LightYellow;
break;
case "3":
for (int i = 5; i < e.Row.Cells.Count; i++)
e.Row.Cells[i].BackColor = System.Drawing.Color.Orange;
break;
}
}}
protected void GridView1_DataBound(object sender, EventArgs e){
GridView GridView1 = (GridView)sender;
foreach (GridViewRow objRow in GridView1.Rows)
{
for (int i = 5; i < objRow.Cells.Count; i++){
CheckBox chkCheckBox = new CheckBox();
objRow.Cells[i].Controls.Add(chkCheckBox);
if (objRow.Cells[i].BackColor == System.Drawing.Color.LightGreen)
chkCheckBox.Checked = true;
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
}