I am trying to create a editable grid in ASP.Net,
In my SQL table, their is a column IsCountyEnabled(datatype bit) and this i want to show as True / False in my grid (After binding the grid i am able to see it as True / False in Active Column).
When the user want to update the column it will be done through Radio Button. I am not able to display radio button for IsCountyEnabled when user edit the row with appropriate radio button selected as true and false if IsCountyEnabled is 0/1.
When i am binding the grid i can see false/true in grid as per the bit value but no idea how to display it in Edit Command and update Command.
My UI code looks like as follows :-
<asp:GridView ID="ResultGridView" runat="server" AutoGenerateColumns="False" ShowFooter="true"
DataKeyNames="CountyID"
AllowPaging="True"
CellPadding="3"
OnPageIndexChanging="ResultGridView_PageIndexChanging"
OnRowDeleting="ResultGridView_RowDeleting"
CssClass="mGrid"
OnRowEditing="ResultGridView_RowEditing"
OnRowUpdating="ResultGridView_RowUpdating"
OnRowCancelingEdit="ResultGridView_RowCancelingEdit"
PageSize="15" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2"
OnRowCommand="ResultGridView_RowCommand" >
<Columns>
<%----%>
<asp:BoundField DataField="CountyID" HeaderText="County ID" InsertVisible="False"
ReadOnly="True" SortExpression="CountyID" Visible="false" />
<%----%>
<asp:TemplateField HeaderText="County Name" SortExpression="CountyName">
<EditItemTemplate>
<asp:TextBox ID="txtCounty" Width="250px" runat="server" Text='<%# Bind("CountyName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCounty1" runat="server" Width="250px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("CountyName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Active" SortExpression="IsCountyEnabled">
<EditItemTemplate >
<asp:RadioButtonList ID="RadioButtonList1" runat="server" >
<asp:ListItem>True</asp:ListItem>
<asp:ListItem>False</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
<FooterTemplate>
<asp:RadioButtonList ID="RadioButtonList2" runat="server" >
<asp:ListItem>True</asp:ListItem>
<asp:ListItem>False</asp:ListItem>
</asp:RadioButtonList>
</FooterTemplate>
<ItemTemplate >
<asp:Label ID="Label4" runat="server" Text='<%# Bind("IsCountyEnabled") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
<%----%>
</Columns>
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<%----%>
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
Code Behind as :-
private void FillCountyGrid()
{
dataTable = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "select countyid,countyname,iscountyenabled from county where regionprojectid= " + CountyID.ToString();
ad = new SqlDataAdapter(cmd);
ad.Fill(dataTable);
ResultGridView.DataSource = dataTable;
ResultGridView.DataBind();
}
protected void ResultGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
ResultGridView.EditIndex = e.NewEditIndex;
FillCountyGrid();
}
protected void ResultGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ResultGridView.PageIndex = e.NewPageIndex;
FillCountyGrid();
}
protected void ResultGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM County WHERE CountyID='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
FillCountyGrid();
}
protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtCounty = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCounty");
RadioButtonList RadioButtonList1 = (RadioButtonList)ResultGridView.Rows[e.RowIndex].FindControl("RadioButtonList1");
cmd.Connection = conn;
cmd.CommandText = "UPDATE County SET Countyname ='" + txtCounty.Text + "' WHERE CountyID='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
ResultGridView.EditIndex = -1;
FillCountyGrid();
conn.Close();
}
protected void ResultGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
ResultGridView.EditIndex = -1;
FillCountyGrid();
}
protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{
TextBox txtRegion1 = (TextBox)ResultGridView.FooterRow.FindControl("txtCounty1");
RadioButtonList txtNatureOFWork1 = (RadioButtonList)ResultGridView.FooterRow.FindControl("RadioButtonList2");
if (txtRegion1.Text != "")
{
cmd.Connection = conn;
conn.Open();
cmd.ExecuteNonQuery();
}
FillCountyGrid();
conn.Close();
}
}