Click here to Skip to main content
16,020,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
LINQ Query For Update Operation in Gridview
plz help..
Posted

1 solution

Hi,

There is no LINQ query for updating the GirdView, rather you have to update the Datasource using LINQ and rebind the GirView.

check below links, for updating the datatable using LINQ,
Update DataTable using LINQ in C#[^]
how to do bulk update using linq[^]

LINQ will update 1 column at a time, if you want to update muultiple columns, repeat the code as shown below.

C#
objDataTable.AsEnumerable().ToList().ForEach(p => p.SetField<int>("COlumn1", 10));
objDataTable.AccectChanges();
objDataTable.AsEnumerable().ToList().ForEach(p => p.SetField<string>("COlumn2", "New Value"));
objDataTable.AccectChanges();

//as many columns you want to update.


if you want to update only selected rows based on the condition try as below.

C#
objDataTable.AsEnumerable().Where(c => c.Fiels<int>("Coloumn1") == 10).ToList().ForEach(p => p.SetField<int>("COlumn1", 10));
objDataTable.AccectChanges();
objDataTable.AsEnumerable().Where(c => c.Fiels<int>("Coloumn1") == 10).ToList().ForEach(p => p.SetField<string>("COlumn2", "New Value"));
objDataTable.AccectChanges();

//as many columns you want to update.
//This will update the rows for which column1 = 10


Later bind the gird view

C#
GridView1.DataSource = objDataTable;
GridView1.DataBind();


hope it helps.
 
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