【问题标题】:How to bind the records into DataGridView?如何将记录绑定到 DataGridView?
【发布时间】:2015-05-22 07:38:10
【问题描述】:

Winforms 应用程序

代码:

        var proxy = new ServiceNow_incident
        {
            Url = "https://instance.service-now.com/change_request.do?SOAP",
            Credentials = new NetworkCredential("Username", "Password")
        };

        var objRecord = new getRecords
        {
            number = "CH1****234"
        };

        var recordsResults = proxy.getRecords(objRecord);

        //grdGetRecords is DataGridView.

getRecords 的类型为 getRecordsResponseGetRecordsResult[]。我想将recordsResults 绑定到grdGetRecords - DataGridView 控件headers

【问题讨论】:

  • 将其解析为一些 IEnumerable,如 ObservableCollection、List 或其他东西,然后将其绑定到您的 DataGrid
  • grdGetRecords.DataSource = recordsResults;
  • 这可能会有所帮助:msdn.microsoft.com/en-us/library/fbk67b6z(v=vs.90).aspx - 我怀疑您可能需要将您的 recordsResults 转换为其他内容,尽管将其传递给 BindingSource。我会先按原样尝试,如果它不起作用,然后进入数据表。

标签: c# .net c#-4.0 datagridview datatable


【解决方案1】:

首先将记录结果设为公共属性:

public getRecordsResponseGetRecordsResult[] RecordsResults { get; set; }

通过ItemsSource中的Binding在xaml页面中秒绑定:

<DataGridView ItemsSource="{Binding RecordsResults}" />

如果您想从源或视图进行动态更新,请务必将 Binding 放入 TwoWay 并将属性设为 ObservableCollection。

希望对您有所帮助!

【讨论】:

  • winforms application
【解决方案2】:

我将recordsResults 设置为grdGetRecordsDataSource 属性。

代码:

var recordsResults = proxy.getRecords(objRecord);
grdGetRecords.DataSource = recordsResults; // Code added for data binding

现在,记录已绑定到DataGridView控件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-28
    • 2013-01-01
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多