使用ComboBox的数据宾对象winforms datagridView

发布于 2025-01-26 18:51:26 字数 1704 浏览 3 评论 0原文

当前,我正在尝试将数据属性属性数据datagridView。问题是DataGridView应该具有一个Combobox列,用户必须在两个值之间进行选择。

所讨论的类是:

 public class SingleTableRow
    {

    /// <summary>
    /// Input für datagrid
    /// </summary>
    public string PropertyName { get; set; }
    public string VaultProperty { get; set; }
    public string SageProperty { get; set; }
    public bool IsSync { get; set; }
    public bool AreSame { get; set; }
    public bool IsSageLeading { get; set; }
    /// <summary>
    /// Single Table row constructor
    /// </summary>
    /// <param name="allValues"></param>
    public SingleTableRow(List<string> allValues)
    {
        PropertyName = allValues[0];
        VaultProperty = allValues[1];
        SageProperty = allValues[2];
        IsSync = false;
        IsSageLeading = true;
        AreSame = VaultProperty.Equals(SageProperty);


    }
}

除了ISSYNC以外的所有值都应绑定到DataGridView。 IssageLeading是一个布尔值,并指示,两个值应为用户预选(保险库或鼠尾草)。

完成后,它应该看起来像这样:

”

我已经有一个实现,但是它仅通过为字符串值创建数据来使用,然后在此之后添加一个带有组合框的列。这显然是不好的设计,因为最后一列没有被整理,也不包含对象的值。

        dtgrid.DataSource = table;
            .
            .
            .
        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
        combo.HeaderText = "Führendes System";
        combo.Name = "Führendes System";

        String[] options = { "Vault", "Sage" };
        combo.Items.AddRange(options);
        dtgrid.Columns.Add(combo);

如何最好地适应这个问题?

Currently I'm trying to databind object properties to a datagridview. The thing is that the datagridview should have a combobox column, where the user has to choose between two values.

The class in question is this one:

 public class SingleTableRow
    {

    /// <summary>
    /// Input für datagrid
    /// </summary>
    public string PropertyName { get; set; }
    public string VaultProperty { get; set; }
    public string SageProperty { get; set; }
    public bool IsSync { get; set; }
    public bool AreSame { get; set; }
    public bool IsSageLeading { get; set; }
    /// <summary>
    /// Single Table row constructor
    /// </summary>
    /// <param name="allValues"></param>
    public SingleTableRow(List<string> allValues)
    {
        PropertyName = allValues[0];
        VaultProperty = allValues[1];
        SageProperty = allValues[2];
        IsSync = false;
        IsSageLeading = true;
        AreSame = VaultProperty.Equals(SageProperty);


    }
}

All values except IsSync should be bound to the Datagridview. The IsSageLeading is a Boolean and indicates, which of the two values should be preselected for the user (Vault or Sage).

Once it is done it should look like this:

Datagridview

I already have a implementation but it only works by creating a DataTable for the String values and then adding a column with combo boxes after that. This is obviously bad design because the last column does not get sorted and does not contain the values of the object.

        dtgrid.DataSource = table;
            .
            .
            .
        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
        combo.HeaderText = "Führendes System";
        combo.Name = "Führendes System";

        String[] options = { "Vault", "Sage" };
        combo.Items.AddRange(options);
        dtgrid.Columns.Add(combo);

How do I apporach this problem best?

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

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

发布评论

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

评论(1

破晓 2025-02-02 18:51:26

听起来您需要将boolean值从IssageLeading映射到要显示的两个值之一(Vault或Sage)。正如您已经指出的那样,由于组合框列不是数据源的一部分,因此它独立于网格数据源起作用。

下面的代码应允许您将IssageLeading属性绑定到组合框列,以便false值将显示为“ Vault”和true值将显示为“鼠尾草”。

我们需要关注的组合框列的三个属性……第一个是列datapropertyname。这将指向网格数据源中的列……即boolean issageLeading属性。

接下来,我们需要为组合框列创建数据源。此数据源将是DataTable,带有两个字段和两个行。称为valueMem的第一个字段是boolean类型。称为displayMem的第二个字段是String字段。整个表看起来像下面……

ValueMemdisplaymem
falsefalse
truesage

此表将用作组合框列的dataSource 。我们需要为列设置的属性是valueMemberdisplayMember,显然会指向组合框数据源中的两个字段。

下面的代码应使您可以将boolean iSsageLeading属性绑定到组合框列。唯一要注意的是,您需要在设置网格数据源之前添加组合框列。

private void Form1_Load(object sender, EventArgs e) {
  dataGridView1.Columns.Add(GetComboColumn());
  dataGridView1.DataSource = GetGridData();
  dataGridView1.Columns["Fuhrendes System"].DisplayIndex = 3;
}


private DataGridViewComboBoxColumn GetComboColumn() {
  DataTable dt = new DataTable();
  dt.Columns.Add("ValueMem", typeof(bool));
  dt.Columns.Add("DisplayMem", typeof(string));
  dt.Rows.Add(false, "Vault");
  dt.Rows.Add(true, "Sage");
  DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
  col.HeaderText = "Fuhrendes System";
  col.Name = "Fuhrendes System";
  col.DataSource = dt;
  col.DataPropertyName = "IsSageLeading";
  col.ValueMember = "ValueMem";
  col.DisplayMember = "DisplayMem";
  return col;
}

private DataTable GetGridData() {
  DataTable dt = new DataTable();
  dt.Columns.Add("PropertyName", typeof(string));
  dt.Columns.Add("VaultProperty", typeof(string));
  dt.Columns.Add("SageProperty", typeof(string));
  dt.Columns.Add("IsSync", typeof(bool));
  dt.Columns.Add("AreSame", typeof(bool));
  dt.Columns.Add("IsSageLeading", typeof(bool));
  Random rand = new Random();
  for (int i = 1; i < 10; i++) {
    dt.Rows.Add("PropertyName " + i,
      "VaultProperty " + i,
      "SageProperty " + i,
      rand.Next(2) == 1,
      rand.Next(2) == 1,
      rand.Next(2) == 1
    );
  }
  return dt;
}

这应该允许组合框列充当网格数据源的一部分,而不是前面描述的脱离……即,排序和过滤应包括issageLeading列。我希望这有意义并有所帮助。

It sounds like you need to map the Boolean values from IsSageLeading to one of the two values you want to display (Vault or Sage). As you have already noted, that since the combo box column is NOT part of the data source, then it acts independently from the GRIDS data source.

The code below should allow you to bind the IsSageLeading property to the combo box column such that a false value will display as “Vault” and a true value will be displayed as “Sage.”

There are three properties of the combo box column we need to focus on…The first is the columns DataPropertyName. This will point to the column in the GRIDS data source… namely the Boolean IsSageLeading property.

Next, we need to create a data source for the Combo Box Column. This data source will be a DataTable with two fields and two rows. The first field which is called ValueMem is a Boolean type. The second field which is called DisplayMem is a string field. The entire table may look something like below…

ValueMemDisplayMem
FalseVault
TrueSage

This table will be used as a DataSource for the Combo Box Column. The properties we need to set for the column are the ValueMember and DisplayMember which will obviously point to each of the two fields in the combo boxes data source.

The code below should enable you to bind the Boolean IsSageLeading property to the combo box column. About the only thing to note is that you need to add the combo box column BEFORE you set the Grids DataSource.

private void Form1_Load(object sender, EventArgs e) {
  dataGridView1.Columns.Add(GetComboColumn());
  dataGridView1.DataSource = GetGridData();
  dataGridView1.Columns["Fuhrendes System"].DisplayIndex = 3;
}


private DataGridViewComboBoxColumn GetComboColumn() {
  DataTable dt = new DataTable();
  dt.Columns.Add("ValueMem", typeof(bool));
  dt.Columns.Add("DisplayMem", typeof(string));
  dt.Rows.Add(false, "Vault");
  dt.Rows.Add(true, "Sage");
  DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
  col.HeaderText = "Fuhrendes System";
  col.Name = "Fuhrendes System";
  col.DataSource = dt;
  col.DataPropertyName = "IsSageLeading";
  col.ValueMember = "ValueMem";
  col.DisplayMember = "DisplayMem";
  return col;
}

private DataTable GetGridData() {
  DataTable dt = new DataTable();
  dt.Columns.Add("PropertyName", typeof(string));
  dt.Columns.Add("VaultProperty", typeof(string));
  dt.Columns.Add("SageProperty", typeof(string));
  dt.Columns.Add("IsSync", typeof(bool));
  dt.Columns.Add("AreSame", typeof(bool));
  dt.Columns.Add("IsSageLeading", typeof(bool));
  Random rand = new Random();
  for (int i = 1; i < 10; i++) {
    dt.Rows.Add("PropertyName " + i,
      "VaultProperty " + i,
      "SageProperty " + i,
      rand.Next(2) == 1,
      rand.Next(2) == 1,
      rand.Next(2) == 1
    );
  }
  return dt;
}

This should allow the combo box column to act as a part of the grids data source and not detached as previously described… i.e., sorting and filtering should include the IsSageLeading column. I hope this makes sense and helps.

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