-2

I have a gridview. Here are my columns: The column choose's control is a dropdown with 2 values: yes/no

No Name Choose
1  Meh  Yes/No

On edit event I want to insert the selected value of the dropdown in the database of the current edited cell.

Inzi Irina
  • 267
  • 2
  • 5
  • 14

2 Answers2

0
protected void GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{   
   GridViewRow row = ((GridView)sender).Rows[e.RowIndex];
   DropDownList ddl =  (DropDownList)row.FindControl("DdlChoose");
   bool yes = ddl.SelectedValue == "Yes";
   SaveData(params); // pseudo-code 
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • what's with the pseudo-code? and the only thing I want to do..and I've tried for 4 days and still didn't succeed is to choose a value in the dropdown then save it , so I choosed to save it in the database cell of the edited row and show it if the user is pressing EDIT. – Inzi Irina Mar 25 '12 at 13:35
  • @InziIrina: You are the only one who knows your data model and your dbms. So i cannot show you the code to insert/update the row in the database unless you didn't show us more details. My code shows you to get the selected value of your dropdown. You could also use the [GridViewUpdateEventArgs.NewValues Property](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.newvalues.aspx) which is passed as parameter to `RowUpdating`. – Tim Schmelter Mar 25 '12 at 13:43
  • I've almost never used `SqlDataSource` but you might want to look at [MSDN](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.updatecommand.aspx) or [similar questions](http://stackoverflow.com/questions/7680153/gridview-rowupdating-sqldatasource-update-from-codebehind) here. – Tim Schmelter Mar 25 '12 at 13:56
  • well , if you have another idea please tell me . – Inzi Irina Mar 25 '12 at 14:00
0

1st thing first, i'd suggest data binding, and apply changes to the Database as bulks and not one at a time, unless forced to by definition.

2nd, each row have a key, then it is no problem to execute a Update the Row by UPDATE mytable SET choose=new_value WHERE key_column=rowkey.

EDIT : DatagridView Binding to SQL Database example can be found : http://csharp.net-informations.com/datagridview/csharp-datagridview-sql-server.htm

Tomer W
  • 3,395
  • 2
  • 29
  • 44
  • 1.I don't understand what you mean in the first idea and 2.it should always be key_column=rowkey right? like No=rowkey where No is auto incremeting by one , look at my example? thank you – Inzi Irina Mar 25 '12 at 13:40
  • Data binding is a whole concept of taking a DataTable (in memory) and putting it on a Visual GUI Grid... the binding handle the connection between the Memory object and the GUI Grid so when you change values on the grid, the changes are performed on the DataTable. – Tomer W Mar 25 '12 at 13:43
  • that's what I want tomer , if I change the value in the dropdown , the changes are performed in the dataTable. and the dropdown should get the value from the datatable. – Inzi Irina Mar 25 '12 at 13:48
  • how can ..I save the choosed value in the dropdownlist? – Inzi Irina Mar 25 '12 at 14:33
  • dgView.Datasource = myDataTable; – Tomer W Mar 25 '12 at 15:02
  • dgView[e.RowIndex, myYesNoColumn.Index] – Tomer W Mar 28 '12 at 09:56