Click here to Skip to main content
12,406,592 members (49,017 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    GridView1.DataBind(); 
}

Gridview structure (.aspx) is:

<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 10-Jul-13 2:01am
Updated 10-Jul-13 4:37am
v3
Comments
Rohan Leuva 10-Jul-13 8:21am
   
Can you post the gridview definition?
rf_libra 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" >



<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />




Rohan Leuva 10-Jul-13 8:51am
   
Use Improve question above and paste code of whole gridview.
rf_libra 10-Jul-13 10:39am
   
Done.
Rohan Leuva 10-Jul-13 12:32pm
   
From where the data comes into this gridview?
rf_libra 10-Jul-13 16:51pm
   
From my code i.e.,protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{


int index = GridView1.EditIndex;

lblSet.Text = GridView1.Rows[index].Cells[1].Text;

SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True");


using (SqlCommand cmd = new SqlCommand())
{
string sql = "UPDATE dbo.Dwg_Register SET Ref = @Ref, Dwg_Ref = @Dwg_Ref,Title = @Title, Dwg_Received_Date = @Dwg_Received_Date, Rev = @Rev, Trade = @Trade, Type=@Type,Produced_Date=@Produced_Date,Produced_By=@Produced_By,Submittal_Ref=@Submittal_Ref,Issued_To=@Issued_To,Date_Issued = Date_Issued, Purpose = @Purpose, Status=@Status,Action_Date=@Action_Date from dbo.Dwg_Register where DwgRegID=@DwgRegID";

cmd.Connection = con;
cmd.CommandText = sql;

cmd.Parameters.Add(new SqlParameter("@Ref", txtRef.Text));
cmd.Parameters.Add(new SqlParameter("@Dwg_Ref", txtDwgRef.Text));
cmd.Parameters.Add(new SqlParameter("@Title", txtTitle.Text));
cmd.Parameters.Add(new SqlParameter("@Dwg_Received_Date", txtDwgReceivedDate.Text == "" ? DBNull.Value : (object)txtDwgReceivedDate.Text));
cmd.Parameters.Add(new SqlParameter("@Rev", txtRev.Text));
cmd.Parameters.Add(new SqlParameter("@Trade", ddlTrade.Text));
cmd.Parameters.Add(new SqlParameter("@Type", ddlType.Text));// == "" ? DBNull.Value : (object)txtDateReceived.Text));
cmd.Parameters.Add(new SqlParameter("@Produced_Date", txtProducedDate.Text == "" ? DBNull.Value : (object)txtProducedDate.Text));
cmd.Parameters.Add(new SqlParameter("@Produced_By", ddlProducedBy.Text));
cmd.Parameters.Add(new SqlParameter("@Submittal_Ref", txtSubmittalRef.Text));
cmd.Parameters.Add(new SqlParameter("@Issued_To", ddlIssuedTo.Text));
cmd.Parameters.Add(new SqlParameter("@Date_Issued", txtDateIssued.Text == "" ? DBNull.Value : (object)txtDateIssued.Text));
cmd.Parameters.Add(new SqlParameter("@Purpose", ddlPurpose.Text));
cmd.Parameters.Add(new SqlParameter("@Status", ddlStatus.Text));
cmd.Parameters.Add(new SqlParameter("@Action_Date", txtActionDate.Text == "" ? DBNull.Value : (object)txtActionDate.Text));
cmd.Parameters.Add(new SqlParameter("@DwgRegID", int.Parse(lblSet.Text)));

con.Open();

cmd.ExecuteNonQuery();


/*SqlConnection con1 = new SqlConnection("Data Source= MEHDI-PC\\SQLExpress; Initial Catalog=PIMS; Integrated Security=True");

string sql1 = "SELECT * from dbo.Dwg_Register";
con.Open();*/

SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds1 = new DataSet();
da.Fill(ds1);
if (con != null)
{
con.Close();
}
GridView1.DataSource = ds1;
GridView1.DataBind();




lblUpdate.Text = "Record updated sucessfully.";




GridView1.DataBind();
GridView1.EditIndex = -1;
MultiView1.SetActiveView(ViewGrid1);
}
}
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;
    fillgrid();
}
This will work.

Regards.. Laugh | :laugh:
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Refer this URL it can be help full for you...

How to edit n update a row in GRID VIEW[^]
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160730.1 | Last Updated 11 Jul 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100