Click here to Skip to main content
15,944,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.NET
<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> 
        <%--CountyID here--%> 
            <asp:BoundField DataField="CountyID" HeaderText="County ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="CountyID" Visible="false" /> 
            <%--    Countyname Editable here--%> 
            <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" />  
           <%-- <asp:CommandField HeaderText="Select" ShowSelectButton="True"  ShowHeader="True"/> --%> 
 
        </Columns> 
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> 
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> 
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> 
        <%--<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />--%> 
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> 
    </asp:GridView> 


Code Behind as :-

C#
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; 
                //cmd.CommandText = "INSERT INTO RegionAndProjectInfo(RegionProjectName, NatureOFWorkID ) Values('" + txtRegion1.Text + "', '" + ddlnatureOfWork.SelectedValue.ToString() + "')"; 
                conn.Open(); 
                cmd.ExecuteNonQuery(); 
            } 
            FillCountyGrid(); 
            conn.Close(); 
        } 
    } 
Posted
Updated 15-Aug-12 22:03pm
v2

1 solution

Hi,
Refer the below links. Look at the gridview RowDataBound event in code behind and you should find what you need to include in your code.

radiobuttonlist-dropdown-gridview-edit[^]

Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List[^]
 
Share this answer
 
v2
Comments
Dharmenrda Kumar Singh 16-Aug-12 4:06am    
I already checked this but in my case i am facing problem when trying to edit the row as ia m not able to see the grid also when clicking on Edit as not getting how to display radio button list for ISCountyEnabled because the value is 0/1 and i have to display it with True/False selection .
__TR__ 16-Aug-12 4:16am    
I don't see a RowDataBound event in your code.
You need to check if the row state is edit and then assign the value of IsCountyEnabled column to your radio button in RowDataBound event.
Dharmenrda Kumar Singh 16-Aug-12 5:01am    
protected void ResultGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState == DataControlRowState.Edit))
{
RadioButtonList rbtnl = (RadioButtonList)e.Row.FindControl("RadioButtonList1");
rbtnl.SelectedValue = drv[2].ToString();
}
}
} Added this but still not getting
__TR__ 16-Aug-12 11:52am    
Can you explain what's happening when you click on "Edit".Are you getting any error?
Did you try debugging your code. What value do you see for "drv[2].ToString()" in debug mode?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900