Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get field value of selected Row Devexpress GridView?

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!!!

like image 750
vyclarks Avatar asked Oct 06 '13 06:10

vyclarks


1 Answers

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").

like image 184
Alex Avatar answered Oct 13 '22 13:10

Alex