0

これが私のフォームのイメージです。

これはこのように行う必要があります。
1. ユーザーは、名前を変更するアイテムを選択します。(例: ユーザーが「string1」を
選択) 2. ドロップダウンで新しい項目名を選択します。(例: ユーザーが「新しい文字列 1」を選択)
注: ドロップダウンで選択した項目は、「string1」の新しい名前になります
。 3. をクリックします。ボタンをクリックします (ボタンをクリックすると、行われた変更が自動的に作成されます)。

ここに画像の説明を入力

ここに問題があります:
ドロップダウンで選択されたアイテムからデータグリッドビューのアイテム名を変更するにはどうすればよいですか?

ここに私のコードのいくつかがあります。

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 に答える