使用 DataGridViewRow.DataBoundItem - 转换为可枚举对象?

发布于 2024-10-01 19:42:45 字数 1367 浏览 0 评论 0原文

我有一个带有

myGridView.DataSource = GetSomeData()


// method called
public IQueryable GetSomeData()
{
    var source = from r in records
                 select r;

    return source;    // made correction from 'r' to 'source'
}

GetSomeData() 的 DataGridView 按预期填充 DataGridView。用户将选择要编辑的行,然后将行数据传递到表单。由于DataGridViewRow.DataBoundItem是匿名类型,我如何传递DataBoundItem

我一半期望我的 DataBoundItem 将是 IQueryable - 不正确。以下是来自调试器的 DataBoundItem 属性信息:

DataBoundItem { 客户ID = "3133", 姓氏 =“史密斯”,名字 = "约翰", 帐号 = "JS3133", ActiveYN = True } <<匿名类型>

一旦数据传递到新表单,我想做一些类似的事情:

txtFName.Text = SomeEnumeratedObject["First_Name"];
txtLName.Text = SomeEnumeratedObject["Last_Name"];

关于如何做到这一点有什么想法吗?在我看来,如果新表单上的控件能够以某种方式绑定到 SomeEnumeratedObject,那就更好了。

是否可以使用 LINQ 查询 DataBoundItem

编辑:

更改的方法:

public DataView GetSomeData()
{
    var source = from r in records
                 select r;

    DataTable table = ToDataTable(myContext, source);
    return new DataView(table);
}

请参阅完整的解决方案此处

I have a DataGridView with

myGridView.DataSource = GetSomeData()


// method called
public IQueryable GetSomeData()
{
    var source = from r in records
                 select r;

    return source;    // made correction from 'r' to 'source'
}

GetSomeData() fills the DataGridView as expected. The user will select a row to edit which then passes the row data to a form. Since the DataGridViewRow.DataBoundItem is an anonymous type, how can I pass DataBoundItem?

I half expected that my DataBoundItem would be IQueryable - incorrect. Here is the info from the debugger on the DataBoundItem property:

DataBoundItem { CustomerID = "3133",
Last_Name = "Smith", First_Name =
"John", AccountNumber = "JS3133",
ActiveYN = True } < Anonymous Type >

Once the data is passed to the new form, I would like to do something like:

txtFName.Text = SomeEnumeratedObject["First_Name"];
txtLName.Text = SomeEnumeratedObject["Last_Name"];

Any ideas on how I can do this? It would be even better, IMO, if the controls on the new form could some how be bound to SomeEnumeratedObject.

Would it possible to query the DataBoundItem with LINQ?

Edit:

Changed method:

public DataView GetSomeData()
{
    var source = from r in records
                 select r;

    DataTable table = ToDataTable(myContext, source);
    return new DataView(table);
}

See complete solution here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临风闻羌笛 2024-10-08 19:42:45

记录中包含什么类型的对象?您应该能够将 DataGridViewRow.DataBoundItem 转换为任何类型的对象记录。

假设记录是 Customer 对象的列表。你应该能够:

txtFName.Text = ((Customer)DataGridViewRow.DataBoundItem).First_Name;
txtLName.Text = ((Customer)DataGridViewRow.DataBoundItem).Last_Name; 

如果那不可能,那么我认为你将不得不使用反射:

Type type = DataGridViewRow.DataBoundItem.GetType();  
String firstName = (String)type.GetProperty("First_Name")
                        .GetValue(DataGridViewRow.DataBoundItem, null);

What type of objects are in records? You should be able to cast DataGridViewRow.DataBoundItem to whatever type of object records holds.

Say records is a list of Customer objects. You should be able to go:

txtFName.Text = ((Customer)DataGridViewRow.DataBoundItem).First_Name;
txtLName.Text = ((Customer)DataGridViewRow.DataBoundItem).Last_Name; 

If that isn't possible, then I think you will have to use reflection:

Type type = DataGridViewRow.DataBoundItem.GetType();  
String firstName = (String)type.GetProperty("First_Name")
                        .GetValue(DataGridViewRow.DataBoundItem, null);
一袭白衣梦中忆 2024-10-08 19:42:45

您可以将其作为动态传递并以这种方式访问​​属性:

public void Test(dynamic item)
{
    MessageBox.Show(string.Format("{0} : {1}", item.First_Name, item.Last_Name));
    textBox1.DataBindings.Add("Text", _item, "First_Name");
    textBox2.DataBindings.Add("Text", _item, "Last_Name");
}

需要考虑的一件事是匿名类型的属性是只读的,因此您的用户将无法以这种方式编辑值。来自 C# 语言规范:

匿名类型的成员是从用于创建该类型实例的匿名对象初始值设定项推断出的一系列只读属性。

您应该考虑为此声明一个类型,而不是使用匿名类型。这样,您还可以受益于智能感知,并能够毫无问题地重命名/重构您的属性。

You could pass it as dynamic and access the properties that way:

public void Test(dynamic item)
{
    MessageBox.Show(string.Format("{0} : {1}", item.First_Name, item.Last_Name));
    textBox1.DataBindings.Add("Text", _item, "First_Name");
    textBox2.DataBindings.Add("Text", _item, "Last_Name");
}

One thing to consider is that properties on anonymous types are read only, so your user will not be able to edit the values that way. From the C# Language Spec:

The members of an anonymous type are a sequence of read-only properties inferred from the anonymous object initializer used to create an instance of the type.

You should consider declaring a type for this instead of using an anonymous type. That way you'll also get the benefit of intellisense and be able to rename/refactor your properties without any problems.

两个我 2024-10-08 19:42:45

kevev22 和 kevev22 adrift 有正确的想法:创建一个类来保存数据。由于我可以将 IQueryable 结果集转换为 DataTable,因此这样做才有意义:

public DataView GetSomeData()
{
    var source = from r in records
                 select r;

    DataTable table = ToDataTable(myContext, source);
    return new DataView(table);
}

现在:

myDGView.DataSource = GetSomeData();

并且,当选择数据网格行时,您可以将 DataBoundItem 转换为 DataRow

仅供参考 - 将 IQueryable 结果集转换为 DataTable:

public DataTable ToDataTable(DataContext context, IQueryable source)
{
    DataTable table = new DataTable();
    {
        adapter.SelectCommand = context.GetCommand(source);
        sqlCommand.Connection.Open();
        adapter.FillSchema(table, SchemaType.Source);
        adapter.Fill(table);
    }

    return table;
}

Both kevev22 & adrift had the right idea: create a class to hold the data. Since I can transform an IQueryable resultset to a DataTable, it only made sense to do that:

public DataView GetSomeData()
{
    var source = from r in records
                 select r;

    DataTable table = ToDataTable(myContext, source);
    return new DataView(table);
}

Now:

myDGView.DataSource = GetSomeData();

And, when a datagrid row is selected, you can cast the DataBoundItem to DataRow.

FYI - to transform an IQueryable resultset to a DataTable:

public DataTable ToDataTable(DataContext context, IQueryable source)
{
    DataTable table = new DataTable();
    {
        adapter.SelectCommand = context.GetCommand(source);
        sqlCommand.Connection.Open();
        adapter.FillSchema(table, SchemaType.Source);
        adapter.Fill(table);
    }

    return table;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文