how to make a Gridview editable / ie. a user can modify the contents of the particular row by clicking on that row ?

What v write in this Event ??
and is this sufficient to make a grid editable??

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    e.NewEditIndex 
}

Plz...help me out with proper code....am at my wits end searching for it :(
cya
Rohan

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Use a DataGridView instead of a GridView.
Editing should be straightforward, click a cell and start to type.

but its a webapplication..n in web application its a Gridview n not Datagrid...i guess :) plz suggest...
cya
Rohan

Use a DataGridView instead of a GridView.
Editing should be straightforward, click a cell and start to type.

Hey here is my fully functional GridView with edit functionality.
To make gridview editable follow the code in GridViewSiteConfiguration_RowEditing method shown below:

If you are confused of this much of code, don't worry, i will give you simple advice:
The thing is extremely simple:
1. Set the edit index of gridview to new editindex which comes from e argument of the method as follows:

GridViewSiteConfiguration.EditIndex = e.NewEditIndex;

2. And after that load the gridview again like this:

loadGridView();

3. and don't forget to define the EditItemtemplate in the gridview as i have defined below

Use you brain and make necessary adjustmenst and you will get it

<asp:GridView ID="GridViewSiteConfiguration" runat="server" CellPadding="4" ForeColor="#333333"
                DataKeyNames="id" GridLines="None" AutoGenerateColumns="false" BorderColor="#E5E5E5"
                BorderStyle="Solid" BorderWidth="1px" OnRowEditing="GridViewSiteConfiguration_RowEditing"
                OnRowCancelingEdit="GridViewSiteConfiguration_RowCancelingEdit" OnRowUpdating="GridViewSiteConfiguration_RowUpdating">
                <RowStyle BackColor="#EFF3FB" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#99CCFF" />
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table class="ViewPagesTable">
                                <tr>
                                    <td class="FirstCol">
                                        Config Key
                                    </td>
                                    <td class="SecondCol">
                                        Value
                                    </td>
                                    <td class="ThirdCol">
                                        Options
                                    </td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table class="ViewPagesTable">
                                <tr>
                                    <td class="FirstCol">
                                        <%# Eval("ConfigKey") %>
                                    </td>
                                    <td class="SecondCol">
                                        <%# Eval("Value") %>
                                    </td>
                                    <td class="ThirdCol">
                                        <asp:LinkButton ID="linkButtonEdit" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <table class="ViewPagesTable">
                                <tr>
                                    <td class="FirstCol">
                                        <asp:Label ID="lblId" runat="server" Visible="false" Text='<%# Eval("Id") %>'></asp:Label>
                                        <asp:TextBox CssClass="SiteConfigTextBoxes" ID="txtConfigKey" MaxLength="50" runat="server"
                                            Text='<%# Eval("ConfigKey") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtConfigKey"
                                            Text="*" ErrorMessage="ConfigKey field cannot be empty."></asp:RequiredFieldValidator>
                                    </td>
                                    <td class="SecondCol">
                                        <asp:TextBox CssClass="SiteConfigTextBoxes" ID="txtValue" MaxLength="8000" runat="server"
                                            Text=' <%# Eval("Value") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtValue"
                                            Text="*" ErrorMessage="Value field cannot be empty."></asp:RequiredFieldValidator>
                                    </td>
                                    <td class="ThirdCol">
                                        <asp:Button ID="btnUpdate" runat="server" CssClass="SiteConfigButtons" Text="Update"
                                            CommandName="Update" CausesValidation="true" />
                                        <asp:Button ID="btnCancel" runat="server" CssClass="SiteConfigButtons" Text="Cancel"
                                            CommandName="Cancel" CausesValidation="false" />
                                    </td>
                                </tr>
                            </table>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

and its corresponding code behind is as follows

private void loadGridView()
    {
        GridViewSiteConfiguration.DataSource = SiteConfiguration.GetAll();
        GridViewSiteConfiguration.DataBind();
    }
    protected void GridViewSiteConfiguration_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewSiteConfiguration.EditIndex = e.NewEditIndex;
        loadGridView();
    }
    protected void GridViewSiteConfiguration_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewSiteConfiguration.EditIndex = -1;
        loadGridView();        
    }
    protected void GridViewSiteConfiguration_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lblId = GridViewSiteConfiguration.Rows[e.RowIndex].FindControl("lblId") as Label;
        TextBox txtConfigKey = GridViewSiteConfiguration.Rows[e.RowIndex].FindControl("txtConfigKey") as TextBox;
        TextBox txtValue = GridViewSiteConfiguration.Rows[e.RowIndex].FindControl("txtValue") as TextBox;

        SiteConfiguration sc = SiteConfiguration.FindSingle(p=>p.Id == Convert.ToInt32(lblId.Text));
        sc.ConfigKey = txtConfigKey.Text.Trim();
        sc.Value = txtValue.Text.Trim();
        sc.Save();

        GridViewSiteConfiguration.EditIndex = -1;
        loadGridView();
    }

If you have any problems contact me at
<snip email and fake signature>

thanks for ur reply...
will def. try n let u knw....

cya
Rohan

Hey here is my fully functional GridView with edit functionality.
To make gridview editable follow the code in GridViewSiteConfiguration_RowEditing method shown below:

If you are confused of this much of code, don't worry, i will give you simple advice:
The thing is extremely simple:
1. Set the edit index of gridview to new editindex which comes from e argument of the method as follows:

GridViewSiteConfiguration.EditIndex = e.NewEditIndex;

2. And after that load the gridview again like this:

loadGridView();

3. and don't forget to define the EditItemtemplate in the gridview as i have defined below

Use you brain and make necessary adjustmenst and you will get it

<asp:GridView ID="GridViewSiteConfiguration" runat="server" CellPadding="4" ForeColor="#333333"
                DataKeyNames="id" GridLines="None" AutoGenerateColumns="false" BorderColor="#E5E5E5"
                BorderStyle="Solid" BorderWidth="1px" OnRowEditing="GridViewSiteConfiguration_RowEditing"
                OnRowCancelingEdit="GridViewSiteConfiguration_RowCancelingEdit" OnRowUpdating="GridViewSiteConfiguration_RowUpdating">
                <RowStyle BackColor="#EFF3FB" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#99CCFF" />
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <table class="ViewPagesTable">
                                <tr>
                                    <td class="FirstCol">
                                        Config Key
                                    </td>
                                    <td class="SecondCol">
                                        Value
                                    </td>
                                    <td class="ThirdCol">
                                        Options
                                    </td>
                                </tr>
                            </table>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <table class="ViewPagesTable">
                                <tr>
                                    <td class="FirstCol">
                                        <%# Eval("ConfigKey") %>
                                    </td>
                                    <td class="SecondCol">
                                        <%# Eval("Value") %>
                                    </td>
                                    <td class="ThirdCol">
                                        <asp:LinkButton ID="linkButtonEdit" runat="server" Text="Edit" CommandName="Edit"></asp:LinkButton>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <table class="ViewPagesTable">
                                <tr>
                                    <td class="FirstCol">
                                        <asp:Label ID="lblId" runat="server" Visible="false" Text='<%# Eval("Id") %>'></asp:Label>
                                        <asp:TextBox CssClass="SiteConfigTextBoxes" ID="txtConfigKey" MaxLength="50" runat="server"
                                            Text='<%# Eval("ConfigKey") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtConfigKey"
                                            Text="*" ErrorMessage="ConfigKey field cannot be empty."></asp:RequiredFieldValidator>
                                    </td>
                                    <td class="SecondCol">
                                        <asp:TextBox CssClass="SiteConfigTextBoxes" ID="txtValue" MaxLength="8000" runat="server"
                                            Text=' <%# Eval("Value") %>'></asp:TextBox>
                                        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtValue"
                                            Text="*" ErrorMessage="Value field cannot be empty."></asp:RequiredFieldValidator>
                                    </td>
                                    <td class="ThirdCol">
                                        <asp:Button ID="btnUpdate" runat="server" CssClass="SiteConfigButtons" Text="Update"
                                            CommandName="Update" CausesValidation="true" />
                                        <asp:Button ID="btnCancel" runat="server" CssClass="SiteConfigButtons" Text="Cancel"
                                            CommandName="Cancel" CausesValidation="false" />
                                    </td>
                                </tr>
                            </table>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

and its corresponding code behind is as follows

private void loadGridView()
    {
        GridViewSiteConfiguration.DataSource = SiteConfiguration.GetAll();
        GridViewSiteConfiguration.DataBind();
    }
    protected void GridViewSiteConfiguration_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewSiteConfiguration.EditIndex = e.NewEditIndex;
        loadGridView();
    }
    protected void GridViewSiteConfiguration_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridViewSiteConfiguration.EditIndex = -1;
        loadGridView();        
    }
    protected void GridViewSiteConfiguration_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label lblId = GridViewSiteConfiguration.Rows[e.RowIndex].FindControl("lblId") as Label;
        TextBox txtConfigKey = GridViewSiteConfiguration.Rows[e.RowIndex].FindControl("txtConfigKey") as TextBox;
        TextBox txtValue = GridViewSiteConfiguration.Rows[e.RowIndex].FindControl("txtValue") as TextBox;

        SiteConfiguration sc = SiteConfiguration.FindSingle(p=>p.Id == Convert.ToInt32(lblId.Text));
        sc.ConfigKey = txtConfigKey.Text.Trim();
        sc.Value = txtValue.Text.Trim();
        sc.Save();

        GridViewSiteConfiguration.EditIndex = -1;
        loadGridView();
    }

SNIPPED

This article has been dead for over six months. Start a new discussion instead.