Row Updation In Grid View Using C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcaamit
    New Member
    • Mar 2008
    • 1

    Row Updation In Grid View Using C#

    I want to update a row in grid view using Bound column
    my code is like this :::But it does`nt work

    int iRowIndex = e.RowIndex;
    string sOrderNo = GridView1.Rows[iRowIndex].Cells[1].Text;

    string sOrdeDate = GridView1.Rows[iRowIndex].Cells[2].Text.ToString( );
    it does`nt gives us value of cell???????
  • saran23
    New Member
    • Mar 2008
    • 28

    #2
    Originally posted by mcaamit
    I want to update a row in grid view using Bound column
    my code is like this :::But it does`nt work

    int iRowIndex = e.RowIndex;
    string sOrderNo = GridView1.Rows[iRowIndex].Cells[1].Text;

    string sOrdeDate = GridView1.Rows[iRowIndex].Cells[2].Text.ToString( );
    it does`nt gives us value of cell???????

    Instead of e.RowIndex try GrdviewName.Sel ectedIndex,
    Hope it works.

    Comment

    • sreeraj1984
      New Member
      • Mar 2008
      • 12

      #3
      hi amit,


      I think this code will be useful for u!!!!!!!!!!!!!! !

      This code is used for Updating a database (MS SQL) from a grid view
      This Coantains the code for editing and cancelling


      Anyway try this

      ///////*************** *CODE


      using System;
      using System.Data;
      using System.Configur ation;
      using System.Collecti ons;
      using System.Web;
      using System.Web.Secu rity;
      using System.Web.UI;
      using System.Web.UI.W ebControls;
      using System.Web.UI.W ebControls.WebP arts;
      using System.Web.UI.H tmlControls;
      using System.Data.Sql Client;

      public partial class GridViewTemplat eFieldEdit : System.Web.UI.P age
      {

      SqlConnection connection;
      SqlCommand cmd;

      protected void Page_Load(objec t sender, EventArgs e)
      {
      if (!Page.IsPostBa ck)
      {
      ConnectAndBound ();
      }


      }
      public void ConnectAndBound ()
      {
      // connection = new SqlConnection(" server=192.168. 1.2;database=Pr ojectManagement System;user ID=sa; password=sa");
      connection = new SqlConnection(C onfigurationMan ager.Connection Strings["TestConnection String"].ConnectionStri ng);
      connection.Open ();
      cmd = new SqlCommand("dbo .select_Student Demo", connection);
      cmd.CommandType = CommandType.Sto redProcedure;
      DataSet ds = new DataSet();
      SqlDataAdapter sda = new SqlDataAdapter( cmd);
      sda.Fill(ds);
      GV_Student.Data Source = ds;
      GV_Student.Data Bind();
      connection.Clos e();
      }

      protected void GV_Student_RowE diting(object sender, GridViewEditEve ntArgs e)
      {
      GV_Student.Edit Index = e.NewEditIndex;
      ConnectAndBound ();
      }
      protected void GV_Student_Sele ctedIndexChange d(object sender, EventArgs e)
      {

      }
      protected void GV_Student_RowC ancelingEdit(ob ject sender, GridViewCancelE ditEventArgs e)
      {

      GV_Student.Edit Index = -1;
      ConnectAndBound ();
      Response.Write( "Editing Cancelled");
      }
      protected void GV_Student_RowU pdating(object sender, GridViewUpdateE ventArgs e)
      {

      // connection = new SqlConnection(" server=192.168. 1.2;database=Pr ojectManagement System;user ID=sa; password=sa");
      connection = new SqlConnection(C onfigurationMan ager.Connection Strings["TestConnection String"].ConnectionStri ng);
      connection.Open ();
      cmd = new SqlCommand("dbo .Update_Student Demo", connection);
      cmd.CommandType = CommandType.Sto redProcedure;


      GridViewRow row = GV_Student.Rows[e.RowIndex];


      Label lbl_StudentID=( Label)(row.Find Control("lbl_St udentID")as Label);
      int studid = int.Parse(lbl_S tudentID.Text);

      TextBox txt_StudentName =(TextBox)(row. FindControl("tx t_StudentName") as TextBox);
      TextBox txt_email = (TextBox)(row.F indControl("txt _Email") as TextBox);
      TextBox txt_phone = (TextBox)(row.F indControl("txt _Phone") as TextBox);
      TextBox txt_AccountNo = (TextBox)(row.F indControl("txt _AccountNumber" ) as TextBox);


      cmd.Parameters. Add(new SqlParameter("@ studentid", SqlDbType.BigIn t, 8));
      cmd.Parameters[0].Value = studid;

      cmd.Parameters. Add(new SqlParameter("@ studentname", SqlDbType.VarCh ar, 30));
      cmd.Parameters[1].Value = txt_StudentName .Text;

      cmd.Parameters. Add(new SqlParameter("@ email", SqlDbType.VarCh ar, 30));
      cmd.Parameters[2].Value = txt_email.Text;


      cmd.Parameters. Add(new SqlParameter("@ phone", SqlDbType.VarCh ar, 30));
      cmd.Parameters[3].Value = txt_phone.Text;


      cmd.Parameters. Add(new SqlParameter("@ accountno", SqlDbType.VarCh ar, 20));
      cmd.Parameters[4].Value = txt_AccountNo.T ext;

      cmd.ExecuteNonQ uery();
      connection.Clos e();
      Response.Write( "*********D ata Updated******** ***");



      }
      protected void GV_Student_RowU pdated(object sender, GridViewUpdated EventArgs e)
      {

      }
      }







      /*************** *************** ********NIce C#ing

      Comment

      Working...