Click here to Skip to main content
15,896,296 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am trying to change a record mode in a GridView to "Edit-Mode" i.e., when user clicks on Edit button in GridView to edit a specific record.

I using the following code:
C#
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataBind(); 
}


Gridview structure (.aspx) is:

C#
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing="GridView1_RowEditing" DataKeyNames="DwgRegID" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdated="GridView1_RowUpdated" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle 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>


When I click on Edit button in the gridview, instead of changing record state to edit-mode, program crashes.

Please help.

Regards
Posted
Updated 10-Jul-13 4:37am
v3
Comments
Thanks7872 10-Jul-13 8:21am    
Can you post the gridview definition?
[no name] 10-Jul-13 8:44am    
<asp:GridView ID="GridView1" runat="server" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing="GridView1_RowEditing" DataKeyNames="DwgRegID" OnRowUpdating="GridView1_RowUpdating" OnRowDeleting="GridView1_RowDeleting" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdated="GridView1_RowUpdated" >
<alternatingrowstyle backcolor="White" forecolor="#284775">
<editrowstyle backcolor="#999999">
<footerstyle backcolor="#5D7B9D" font-bold="True" forecolor="White">
<HeaderStyle 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">
Thanks7872 10-Jul-13 8:51am    
Use Improve question above and paste code of whole gridview.
[no name] 10-Jul-13 10:39am    
Done.
Thanks7872 10-Jul-13 12:32pm    
From where the data comes into this gridview?

Let me explain you.You tried to fill gridview at the time of updating,that means there would be no data at the time of editing.so you have to first fill your gridview with data.Make function say fillgrid(),and bind your gridview here for first time. When you call your row editing event(OnRowEditing="GridView1_RowEditing"),make change like this
C#
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    fillgrid();
}

This will work.

Regards.. :laugh:
 
Share this answer
 
v2
Refer this URL it can be help full for you...

How to edit n update a row in GRID VIEW[^]
 
Share this answer
 

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