0

这是我的表格的图像。

这应该这样做。
1. 用户将选择要重命名的项目。(例如,用户选择“string1”)
2. 在下拉列表中选择新项目名称。(例如,用户选择“新字符串 1”)
注意:下拉列表中选择的项目将是“string1”的新名称
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;
        }
4

1 回答 1

0

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

                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属性

于 2012-11-13T16:08:01.477 回答