【问题标题】:Manipulating with datagridview and dropdownlist使用 datagridview 和下拉列表进行操作
【发布时间】:2012-11-14 16:51:50
【问题描述】:

这是我的表单的图像。

应该这样做。
1. 用户将选择要重命名的项目。 (例如,用户选择“string1”)
2. 在下拉列表中选择新项目名称。(例如,用户选择“新字符串 1”)
注意:下拉列表中选择的项目将是“字符串 1”的新名称
3. 点击按钮。(点击按钮。这将自动创建所做的更改)。

问题来了:
如何从下拉列表中的选定项目更改 datagridview 上的项目名称?

这是我的一些代码。

public void loadgrid()
        {
            DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
            checkboxColumn.Width = 25;
            dataGridView1.Columns.Add(checkboxColumn);

            DataGridViewTextBoxColumn textboxcolumn = new DataGridViewTextBoxColumn();
            textboxcolumn.Width = 150;
            dataGridView1.Columns.Add(textboxcolumn);

            dataGridView1.Rows.Add(new object[] { false, "string1" });
            dataGridView1.Rows.Add(new object[] { false, "string2" });
        }

按钮1

private void button1_Click(object sender, EventArgs e)
        {
            int x = 0;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            { 
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
                chk.TrueValue = true;
                if(chk.Value == chk.TrueValue) // there no chk.Check on datagridview just chk.Selected
                {
                    //code here
                    //this will test if the checkbox has been checked.
                    //the selected item on the dropdown will be the
                    //new name of the item on the datagridview
                }
            }
                x = x + 1;
        }

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    如果你想用选中的复选框更改行中的所有字符串,我认为这会有所帮助:

                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        if (!row.IsNewRow)
                        {
                            DataGridViewCheckBoxCell chk1 = (DataGridViewCheckBoxCell)row.Cells[0];
                            if (chk1 != null && chk1.Value!=null && (bool)chk1.Value) // there no chk.Check on datagridview just chk.Selected
                            {
                                string newString = string.Empty;
                                if (comboBox1.SelectedItem != null)
                                {
                                    newString = comboBox1.SelectedItem.ToString();
                                    row.Cells[dataGridView1.Columns.Count - 1].Value = newString;
                                }
                            }
                        }
                    }
    

    如果您只想在选中 CheckBox 的选定行中更改字符串,则可以使用以下代码:

                    if(dataGridView1.SelectedRows.Count>0)
                    {
                        DataGridViewRow row = dataGridView1.SelectedRows[0];
                        if (!row.IsNewRow)
                        {
                            DataGridViewCheckBoxCell chk1 = (DataGridViewCheckBoxCell)row.Cells[0];
                            if (chk1 != null && chk1.Value!=null && (bool)chk1.Value) // there no chk.Check on datagridview just chk.Selected
                            {
                                string newString = string.Empty;
                                if (comboBox1.SelectedItem != null)
                                {
                                    newString = comboBox1.SelectedItem.ToString();
                                    row.Cells[dataGridView1.Columns.Count - 1].Value = newString;
                                }
                            }
                        }
                    }
    

    如果您想设置所有选定的行(如果这在您的 DataGridView 控件中是可能的,那么只需迭代 SelectedRows 属性

    【讨论】:

    • 我在发布之前检查了代码,它工作正常。你的错误到底在哪里?
    • (bool) 有错误 - 对象引用未设置为对象的实例。
    • 我也试过这个。布尔 b = 真;然后将 (bool) 更改为 (b)chk1.value 但仍然有错误。
    • 现在检查一下,我更改了代码,但我认为您在将任何内容插入 DataGridView 之前调用了此方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2010-10-14
    • 1970-01-01
    相关资源
    最近更新 更多