C# 如何获取所选行 Devexpress GridView 的字段值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19206186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get field value of selected Row Devexpress GridView?
提问by vyclarks
I use a DevexpressGridView to display all TOPIC (id,title,content)
我使用 DevexpressGridView 来显示所有 TOPIC (id,title,content)
<dx:ASPxGridView ID="gv" runat="server"
OnSelectionChanged="gv_SelectionChanged" >
I have grid_SelectionChanged event:
我有 grid_SelectionChanged 事件:
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.
gv.SelectedRow
DevGridview中似乎不存在方法。
As recommended, I've tried with FocusedRowIndex
method, but I really dont know the right syntax to get the value of selected row.
按照建议,我尝试过使用FocusedRowIndex
方法,但我真的不知道获取所选行值的正确语法。
Help!!!
帮助!!!
采纳答案by vyclarks
I've found my answere here after a long time searching google: http://www.devexpress.com/Support/Center/Question/Details/Q347704
经过长时间的谷歌搜索,我在这里找到了答案:http: //www.devexpress.com/Support/Center/Question/Details/Q347704
Use the ASPxGridView.GetSelectedFieldValuesmethod get selected row values on the server side.
使用ASPxGridView.GetSelectedFieldValues方法在服务器端获取选定的行值。
回答by Alex
Changing the selection is different from changing the focused row. See the documentation for Selection
for the difference between the two.
更改选择与更改焦点行不同。有关Selection
两者之间的区别,请参阅文档。
You can use gv.GetSelectedFieldValues
to get the rows which are selected.
您可以使用gv.GetSelectedFieldValues
来获取所选的行。
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.
FocusedRowChanged
如果您对焦点行感兴趣,您应该处理该事件。
You can use the FocusedRowIndex
value to index the rows of gv.DataSource
, for example:
您可以使用该FocusedRowIndex
值来索引 的行gv.DataSource
,例如:
DataTable ds = (DataTable)gv.DataSource;
var id = ds.Rows[gv.FocusedRowIndex]["id"];
or you can use var id = gv.GetRowValues(gv.FocusedRowIndex, "id")
.
回答by Softec
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
这将返回 DataRow
Please note this is when I am using Devexpress gridControl in WinForms
请注意这是我在 WinForms 中使用 Devexpress gridControl 时
回答by hakan ba?türk
If you want to get only ID field value you can use this
如果您只想获取 ID 字段值,您可以使用它
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;