Click here to Skip to main content
12,395,089 members (66,421 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ADO.NET
i am using datatable as datasource to bind Gridview
but i was done bind the data but how to update and Delete

DataTable Details = new DataTable();
 
           Details.Columns.Add("ID", typeof(Int32));
           Details.Columns.Add("Name", typeof(string));
           Details.Columns.Add("Number", typeof(string));
 
           Details.Rows.Add("1", "Vicks400", "456789456");
           Details.Rows.Add("2", "Nitrose", "468795124");
           Details.Rows.Add("3", "Nitrored", "154657894");
 
           ViewState["Drugs"] = Details;
private void bindGrid()
       {
           DataTable dt = (DataTable)ViewState["Drugs"];
           gvDrug.DataSource = dt;
           gvDrug.DataBind();
 
       }

protected void gvDrug_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = gvDrug.Rows[e.RowIndex];
 
           String str = ((TextBox)(row.Cells[1].Controls[0])).Text;
 
            int id = Convert.ToInt32(e.NewValues[0]);
           
            string name = e.NewValues[1].ToString();
            string numberR = e.NewValues[2].ToString();
 
            DataTable dt = (DataTable)ViewState["Drugs"];
 
            dt.Rows[0]["Name"] = name;
            dt.Rows[0]["Number"] = numberR;
 
            dt.AcceptChanges();
 
            gvDrug.DataSource = dt;
            gvDrug.DataBind();

but above code not updating how to do? need helpfull
Posted 16-Apr-12 22:58pm
Comments
P.Salini 17-Apr-12 4:59am
   
Are you getting any error?
Abhiram KA 17-Apr-12 5:00am
   
not updated not showing errors
Saroj Kumar Sahu 17-Apr-12 5:06am
   
check ur database query for update.
Abhiram KA 17-Apr-12 5:29am
   
here is this sufficient
dt.Rows[0]["Name"] = name;
dt.Rows[0]["Number"] = numberR;

dt.AcceptChanges();
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2
Comments
Prasad Kulkarni (pashad) 17-Apr-12 5:12am
   
Link formatted
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

This code is working Fine

protected void gvDrug_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           string name = e.NewValues[1].ToString();
           string numberR = e.NewValues[2].ToString();
 
           DataTable dt = (DataTable)ViewState["Drugs"];
 
           dt.Rows[e.RowIndex]["Name"] = name;
           dt.Rows[e.RowIndex]["Number"] = numberR;
 
           dt.AcceptChanges();
 
           gvDrug.EditIndex = -1;
           gvDrug.DataSource = dt;
           gvDrug.DataBind();
 
       }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

you can use this

//Fill your data in DataAdapter
SqlDataAdapter da = new SqlDataAdapter(cmdselect);
                DataSet ds = new DataSet();
                da.Fill(ds);
 
//find edited row from gridview in DataSet 
DataRow[] drresult = ds.Tables[0].Select("ID == " + id + "");
 

//if You set PrimaryKey in DataTable you can find data row whith this method
     ds.Tables[0].Rows.Find(ID) 'Your primary key value'
 
//and set datarow value

ds.tables[].rows[0]['Title'] = "value"
 
da.Update(ds);

Edit: code formatted
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Place One button for update
update datatable dt.upadate.
and call bindGrid(); on that click event.
  Permalink  
v2

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
Web02 | 2.8.160721.1 | Last Updated 17 Apr 2012
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