簡體   English   中英

c# winform中選擇DataGridView的一列值

[英]Select a column value of DataGridView in c# winform

我正在 Visual Studio 2010 c# 中開發應用程序。

如圖所示,我有兩種形式:

在此處輸入圖片說明

在 Form2 中,我有一個帶有用戶名的DataGridView控件,在 Form1 中我有一個 TextBox 和一個按鈕。 我通過以下方式打開了 Form2:

Form2 frm = new Form2();
        frm.ShowDialog();

如何在 Form1 TextBox 中獲取 GDataGridView 的選定列值?

試試這個:獲取選定的網格值

if (dataGridView1.SelectedRows.Count != 0)
{
    string selectedval;
    DataGridViewRow row = this.dataGridView1.SelectedRows[0];
    selectedval= row.Cells["ColumnName"].Value
}

定義表單的屬性,然后在其他地方使用它,它可以與表單實例一起使用

public string SetText
{
  get { return textBox1.Text; }
  set { textBox1.Text = value; }
}

您可以使用事件來解決問題。 只需像這樣在您的 form2 中創建一個事件

public event Action<string> DatagridCellSelected; 

在您的 form1 連接中使用此事件的方法。

DatagridCellSelected+=form2_DatagridCellSelected;

在這種方法中做這樣的事情

textbox1.Text = obj;

現在在你的 form2 處理 DataGridView 單元格輸入事件

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    var value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    DatagridCellSelected(value ?? "");
}

有我的變種。 將此屬性添加到帶有數據網格的 Form 類中:

    public DataGridViewCell SelectedCell
    {
        get
        {
            return dataGridView1.SelectedCells.Count > 0 ? dataGridView1.SelectedCells[0] : null;
        }
    }

    public string SelectedValue 
    {
        get
        {

            var val = SelectedCell != null ? SelectedCell.Value : null;
            return val != null ? val.ToString() : null;
        }
        set
        {
            SelectedCell.Value = value;
        }
    }

用法:

form.SelectedValue = "123";

僅當僅選擇一個單元格時,這才會正常工作。

這是一個干凈的代碼,可以解決您的情況

假設您有兩個表單Form1Form2

Form 1textboxButton 按鈕上單擊Form2顯示

Form1.cs

        private void button1_Click(object sender, EventArgs e)
        {
              Form2 f = new Form2();
              f.DataGridCell += new Action<string>(f_DatagridCell);
              f.ShowDialog();
        }

       void f_DatagridCell(string obj)
       {
         textBox1.Text = obj;
       }

並在您的Form2.cs

      public event Action<string> DataGridCell ;


       private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
       {
        try
        {
            if (DatagridCell!=null)
            {
                var value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                DatagridCell(value);

            }

        }
        catch { }
    } 

你完成了:)

根據您的所有建議找到正確答案。

感謝您的幫助。

這是我為所有有需要的人提供的工作代碼。

在 Form1 中

private void BtnSelect_Click(object sender, EventArgs e)
        {
            frm.ShowDialog();
            textBox1.Text= frm._textBox1.ToString();
        }
        public string _textBox
        {
            set { textBox1.Text = value; }
        }

並在 Form2 中

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string val = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            textBox1.Text = val;
            this.Close();
        }
        public string _textBox1
        {
            get { return textBox1.Text.Trim(); }
        }

干杯..!!!!!!!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM