Click here to Skip to main content
15,847,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

C#
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
Comments
P.Salini 17-Apr-12 4:59am    
Are you getting any error?
Abhi 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.
Abhi KA 17-Apr-12 5:29am    
here is this sufficient
dt.Rows[0]["Name"] = name;
dt.Rows[0]["Number"] = numberR;

dt.AcceptChanges();

 
Share this answer
 
v2
Comments
Prasad_Kulkarni 17-Apr-12 5:12am    
Link formatted
This code is working Fine

C#
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();

       }
 
Share this answer
 
 
Share this answer
 
you can use this

C#
//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
 
Share this answer
 
v4
Place One button for update
update datatable dt.upadate.
and call bindGrid(); on that click event.
 
Share this answer
 
v2

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