|
-
Jan 4th, 2010, 12:12 PM
#1
Thread Starter
Member
-
Jan 5th, 2010, 03:20 AM
#2
Re: Bulk Update in Gridview
Hey,
Set the following property to true for the GridView:
http://msdn.microsoft.com/en-us/libr...ditbutton.aspx
Then handle the RowEditing event:
http://msdn.microsoft.com/en-us/libr...owediting.aspx
Hope that helps!!
Gary
-
Jan 5th, 2010, 10:06 AM
#3
Thread Starter
Member
Re: Bulk Update in Gridview
The desired columns in my gridview are already in editable mode as you can see in ScreenShot.
Now i want to do is after filling values in these columns when i click on Save Button which is outside gridview, the database get updated.
-
Jan 5th, 2010, 10:12 AM
#4
Re: Bulk Update in Gridview
Hey,
The problem with the approach that you have though is that you have no way of knowing which rows within the GridView have actually changed, unless you are keeping a track of this separately. Since this is the case, you have no option but to take all the rows in the GridView, and perform an Update statement for each row, which means unnecessary trips to the database. That is why I was suggesting a per row change.
Gary
-
Jan 5th, 2010, 10:31 AM
#5
Thread Starter
Member
Re: Bulk Update in Gridview
I understand your point but my requirement is to update all at once.
and my data is coming from two tables
this is my select query
SelectCommand="SELECT PODetails.ItemID, EstMaterials.MatName, EstMaterials.Qty, EstMaterials.QtyOrdered, EstMaterials.Qty - EstMaterials.QtyOrdered AS Balance, PODetails.Qty AS QtyOrder, PODetails.Price FROM EstMaterials INNER JOIN PODetails ON EstMaterials.MatID = PODetails.ItemID
I want to update only 2 fields of "PODetails" table i.e Qty and Price
-
Jan 5th, 2010, 11:06 AM
#6
Re: Bulk Update in Gridview
Hey,
I understand your requirement, but I still think it is a bad idea. You are adding a lot of updates that don't need to happen, which is my opinion is a bad design. Better to keep a track of which cells actually change, and only update those.
Gary
-
Jan 5th, 2010, 12:01 PM
#7
Thread Starter
Member
Re: Bulk Update in Gridview
But plz can you help, with my current requirement
-
Jan 5th, 2010, 12:39 PM
#8
Re: Bulk Update in Gridview
Hey,
If you are going to stick with this approach, then you would need to do something like the following:
On the click event of the save button, loop through all the rows in the GridView. For each row, find the controls that contain the values that you require. Create a connection to the database, and create a command object that contains the query that you need update the row of the table. Create a parameter for each part of the query, add the value using the controls from the GridView row, and then execute the query.
Gary
-
Jan 5th, 2010, 11:28 PM
#9
Thread Starter
Member
Re: Bulk Update in Gridview
Can you give an sample code for this.
-
Jan 6th, 2010, 03:04 AM
#10
Re: Bulk Update in Gridview
-
Jan 8th, 2010, 03:47 PM
#11
Re: Bulk Update in Gridview
In the save button's click event, loop through the rows. For each row, do a findcontrol. Get the value out, then perform an ExecuteNonQuery against the database for each row. Note that you can't do this with the sqldatasource, you'll have to do it via code.
Code:
foreach (GridViewRow gvrow in GridView1.Rows)
{
TextBox txtQty = gvrow.FindControl("TextBox1") as TextBox;
TextBox txtAmt = gvrow.FindControl("TextBox2") as TextBox;
if(txtQty != null && txtAmt != null)
{
string quantity = txtQty.Text;
string amount = txtAmt.Text;
//Execute a stored procedure
}
}
Example of executing a stored proc:
Code:
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "StoredProcedureName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Param1", quantity);
cmd.Parameters.AddWithValue("@Param2", amount);
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|