尝试从 C# 中的键值对创建 DataGridView

发布于 2024-09-07 11:54:43 字数 1349 浏览 4 评论 0原文

在 Windows 窗体应用程序中,我尝试创建一个具有两列的 DataGridView:一列用于 XML 元素给出的键,一列用于所述 XML 元素的值。到目前为止,这是我的代码:

        this.myData = new DataGridView();
        ((System.ComponentModel.ISupportInitialize)(myData)).BeginInit();

        myData.Location = new System.Drawing.Point(12, 42);
        myData.Name = "myData";
        myData.Size = new System.Drawing.Size(1060, 585);
        myData.TabIndex = 32;

        foreach (XElement xElem in xInfoItems)
        {
            numItems++;
        }

        myData.Columns.Add(new DataGridViewTextBoxColumn());
        myData.Columns.Add(new DataGridViewTextBoxColumn());
        myData.Columns[0].Name = "Key";
        myData.Columns[0].DataPropertyName = "key";
        myData.Columns[1].Name = "Value";
        myData.Columns[1].DataPropertyName = "value";

        List<myRow> data = new List<myRow>();


        foreach (XElement xElem in xInfoItems)
        {
            data.Add(new myRow(xElem.Attribute("key").Value, xElem.Value));
        }

        myData.DataSource = data;

        myData.Refresh();

        this.PerformLayout();

我已经确认 data 中的所有信息都是通过 foreach 加载的,因此该部分正在工作。我的问题是网格显示,但网格上没有显示任何内容。我做错了什么?我不太擅长这种数据类型,所以如果这是显而易见的事情,我深表歉意。

更新

我发现我没有在设计视图中正确设置 myData。添加 myRow 类后,它运行得很好。感谢您的帮助!

In a Windows Form application, I'm trying to create a DataGridView with two columns: one for the key given by an XML element and one for the value of said XML element. This is my code so far:

        this.myData = new DataGridView();
        ((System.ComponentModel.ISupportInitialize)(myData)).BeginInit();

        myData.Location = new System.Drawing.Point(12, 42);
        myData.Name = "myData";
        myData.Size = new System.Drawing.Size(1060, 585);
        myData.TabIndex = 32;

        foreach (XElement xElem in xInfoItems)
        {
            numItems++;
        }

        myData.Columns.Add(new DataGridViewTextBoxColumn());
        myData.Columns.Add(new DataGridViewTextBoxColumn());
        myData.Columns[0].Name = "Key";
        myData.Columns[0].DataPropertyName = "key";
        myData.Columns[1].Name = "Value";
        myData.Columns[1].DataPropertyName = "value";

        List<myRow> data = new List<myRow>();


        foreach (XElement xElem in xInfoItems)
        {
            data.Add(new myRow(xElem.Attribute("key").Value, xElem.Value));
        }

        myData.DataSource = data;

        myData.Refresh();

        this.PerformLayout();

I have confirmed that all of the information in data is being loaded via foreach, so that part is working. My problem is that the grid displays, but nothing shows up on the grid. What am I doing wrong? I'm not very good with this data type so I apologize if it's something obvious.

UPDATE

I figured out that I hadn't set myData up properly in the Design view. After adding the myRow class, it worked perfectly. Thanks for the help!

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

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

发布评论

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

评论(2

鸠魁 2024-09-14 11:54:43

问题可能出在您的 myRow 类中。当我尝试重现您的代码时,我首先将“键”和“值”定义为 myRow 类的公共字段,如下所示:

public class myRow {
  public string key;
  public string value;

  public myRow( string Key, string Value )
  {
     key = Key;
     value = Value;
  }

}

这导致绑定行显示,但文本不在单元格中。当我将它们都更改为属性时,绑定效果更好:

public class myRow{
  private string _key;
  public string key
  {
     get
     {
        return _key;
     }
  }

  private string _value;
  public string value
  {
     get
     {
        return _value;
     }
  }

  public myRow( string Key, string Value )
  {
     _key = Key;
     _value = Value;
  }

}

The problem may lie in your myRow class. When I was trying to reproduce your code, I first defined "key" and "value" as public fields of the myRow class as so:

public class myRow {
  public string key;
  public string value;

  public myRow( string Key, string Value )
  {
     key = Key;
     value = Value;
  }

}

This caused the bound rows to show up but the text was not in the cells. When I changed both of them to properties, the binding worked much better:

public class myRow{
  private string _key;
  public string key
  {
     get
     {
        return _key;
     }
  }

  private string _value;
  public string value
  {
     get
     {
        return _value;
     }
  }

  public myRow( string Key, string Value )
  {
     _key = Key;
     _value = Value;
  }

}

冷月断魂刀 2024-09-14 11:54:43

我对您的代码所做的修改可能会有所帮助。 (我只关注使用数据表创建列和添加行的部分)。

this.myData = new DataGridView();
((System.ComponentModel.ISupportInitialize)(myData)).BeginInit();

myData.Location = new System.Drawing.Point(12, 42);
myData.Name = "myData";
myData.Size = new System.Drawing.Size(1060, 585);
myData.TabIndex = 32;

foreach (XElement xElem in xInfoItems)
{
    numItems++;
}


// Here we create a DataTable with two columns.
DataTable table = new DataTable();
table.Columns.Add("Key", typeof(string));
table.Columns.Add("Value", typeof(string));

foreach (XElement xElem in xInfoItems)
{
     //Here we add rows to table
     table.Rows.Add(xElem.Attribute("key").Value, xElem.Value);
}

myData.DataSource = table;

myData.Refresh();

this.PerformLayout(); 

Probably the modifications I made on your code can help. (I just have focused on the part where you create the columns and add the rows, using a DataTable).

this.myData = new DataGridView();
((System.ComponentModel.ISupportInitialize)(myData)).BeginInit();

myData.Location = new System.Drawing.Point(12, 42);
myData.Name = "myData";
myData.Size = new System.Drawing.Size(1060, 585);
myData.TabIndex = 32;

foreach (XElement xElem in xInfoItems)
{
    numItems++;
}


// Here we create a DataTable with two columns.
DataTable table = new DataTable();
table.Columns.Add("Key", typeof(string));
table.Columns.Add("Value", typeof(string));

foreach (XElement xElem in xInfoItems)
{
     //Here we add rows to table
     table.Rows.Add(xElem.Attribute("key").Value, xElem.Value);
}

myData.DataSource = table;

myData.Refresh();

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