How to get field value of selected Row Devexpress GridView?
Asked Answered
N

4

6

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

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

I have 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 gv.SelectedRow method isn't exist in DevGridview.

As recommended, I've tried with FocusedRowIndex method, but I really dont know the right syntax to get the value of selected row.

Help!!!

Nashville answered 6/10, 2013 at 6:44 Comment(0)
N
1

I've found my answere here after a long time searching google: http://www.devexpress.com/Support/Center/Question/Details/Q347704

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

Nashville answered 6/10, 2013 at 9:15 Comment(0)
I
8

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

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

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

You should handle the FocusedRowChanged event if you're interested in the focused row.

You can use the FocusedRowIndex value to index the rows of gv.DataSource, 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").

Itinerancy answered 6/10, 2013 at 8:45 Comment(2)
gv.Selection give back the row position, is that right? What thing I can do with the position of row? I mean, I have to do what to get the value? Can you tell me some detail? It is so hard for me to work with DevexpressGridViewNashville
There are a lot of quirks when working with DevExpress grids, one of them being that it's a little tricky to get the values of the cells in the grid, mainly because the DataSource isn't strongly typed. I have updated my answer with more details.Itinerancy
B
2

You can also get selected data row as

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

This would return DataRow

Please note this is when I am using Devexpress gridControl in WinForms

Burger answered 2/10, 2014 at 11:34 Comment(0)
H
2

If you want to get only ID field value 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 value method is

int ID = selectedPersonel.ID;
Hypergolic answered 27/7, 2017 at 6:28 Comment(0)
N
1

I've found my answere here after a long time searching google: http://www.devexpress.com/Support/Center/Question/Details/Q347704

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

Nashville answered 6/10, 2013 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.