【发布时间】:2013-04-03 08:46:23
【问题描述】:
我有一个 devexpress XtraGrid 控件。但是,当 winform 加载时,我无法获取默认选定行的 ID。当用户点击网格时,我知道如何获取它。
这里是代码快照:
private void Form1_Load(object sender, EventArgs e)
{
grid1.DataSource = bindData(DataClassesDataContext.Table1.ToList());
ID = Convert.ToInt32(gridView.GetRowCellValue(gridView.FocusedRowHandle, "ID"));
XtraMessageBox.Show(ID.ToString());
}
public BindingSource bindData(object obj)
{
BindingSource ctBinding;
try
{
ctBinding = new BindingSource();
ctBinding.DataSource = obj;
return ctBinding;
}
catch (Exception ex)
{
XtraMessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
}
【问题讨论】:
-
Table1中的类型是什么? -
它是一个使用 LINQ to SQL 访问的表对象
-
您能具体说明您遇到的问题吗?我想我不明白你需要什么。
-
问题是每次加载表单时都会抛出“对象引用未设置为对象的实例”。当我深入研究代码时,问题在于获取焦点行(默认情况下)值。即使列表中有太多行,它也会返回 0(对于 ID)。当我点击行时它工作正常。但是,我希望它在表单加载时获取 ID
-
您真的应该将其添加到您的问题中。如果它返回 0,则您正在查看
RowHandle,而不是行值。尝试将您的代码移动到form_Shown事件而不是form_Load事件
标签: devexpress xtragrid