How to get field value for selected Row Devexpress GridView?

I use DevexpressGridView to display all TOPIC (id,title,content)

 <dx:ASPxGridView ID="gv" runat="server" OnSelectionChanged="gv_SelectionChanged" > 

I have a grid_SelectionChanged event:

 protected void gv_SelectionChanged(object sender, EventArgs e) { int id= selected row...???; //how can I get the value of selected row string sql = "select * from TOPIC where idTOPIC="+id; DataTable topic = l.EXECUTEQUERYSQL(sql); TextBox1.Text = topic.Rows[0][1].ToString(); } 

...

It seems that the gv.SelectedRow method gv.SelectedRow not exist in DevGridview.

As recommended, I tried using the FocusedRowIndex method, but I really don't know the correct syntax to get the value of the selected row.

Help !!!

+6
source share
4 answers

I found my answer here after a long google search: http://www.devexpress.com/Support/Center/Question/Details/Q347704

Use the ASPxGridView.GetSelectedFieldValues method to get the selected row values ​​on the server side.

+1
source

Changing the selection is different from changing the focused row. See Selection Documentation for the difference between the two.

You can use gv.GetSelectedFieldValues to get the selected rows.

 var ids = gv.GetSelectedFieldValues("id"); foreach( var id in ids ) DoSomethingWithObject(id); 

You should handle the FocusedRowChanged event if you are interested in a focused row.

You can use the FocusedRowIndex value to index gv.DataSource strings, for example:

 DataTable ds = (DataTable)gv.DataSource; var id = ds.Rows[gv.FocusedRowIndex]["id"]; 

or you can use var id = gv.GetRowValues(gv.FocusedRowIndex, "id") .

+8
source

You can also get the selected data row as

 int rowHandle = gridView1.FocusedRowHandle; if (rowHandle != DevExpress.XtraGrid.GridControl.InvalidRowHandle) { return this.gridView1.GetDataRow(rowHandle); } 

This will return a DataRow

Please note that this is when I use the Devexpress gridControl in WinForms

+1
source

If you want to get only the value of the ID field, you can use this

 int id = Convert.ToInt32(gv.GetRowValues(gv.FocusedRowIndex, "ID").ToString()); 

If you have an object, you can use this

 Personels selectedPersonel = gv.GetRow(gv.FocusedRowIndex) as Personels; 

and get the value method

 int ID = selectedPersonel.ID; 
0
source

Source: https://habr.com/ru/post/955332/


All Articles