简体   繁体   English

c# winform中选择DataGridView的一列值

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

I am developing an application in Visual Studio 2010 c#.我正在 Visual Studio 2010 c# 中开发应用程序。

I have two forms as shown in this image:如图所示,我有两种形式:

在此处输入图片说明

In Form2 I have a DataGridView control with the user's name, and in Form1 I have a TextBox and a Button.在 Form2 中,我有一个带有用户名的DataGridView控件,在 Form1 中我有一个 TextBox 和一个按钮。 I opened Form2 by:我通过以下方式打开了 Form2:

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

How can I get the selected column value of GDataGridView in the Form1 TextBox?如何在 Form1 TextBox 中获取 GDataGridView 的选定列值?

try this: To get Selected Grid value试试这个:获取选定的网格值

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

Define a property of the form like, then use this in other places it would be available with the form instance定义表单的属性,然后在其他地方使用它,它可以与表单实例一起使用

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

You can use event to solve the issue.您可以使用事件来解决问题。 Just create an event in your form2 like this只需像这样在您的 form2 中创建一个事件

public event Action<string> DatagridCellSelected; 

In your form1 hookup a method with this event.在您的 form1 连接中使用此事件的方法。

DatagridCellSelected+=form2_DatagridCellSelected;

In this method do something like this在这种方法中做这样的事情

textbox1.Text = obj;

Now in your form2 handle DataGridView cell enter event现在在你的 form2 处理 DataGridView 单元格输入事件

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

There is my variant.有我的变种。 Add this properties to your Form class with data grid:将此属性添加到带有数据网格的 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;
        }
    }

Usage:用法:

form.SelectedValue = "123";

This will be work correctly only if selected only one cell.仅当仅选择一个单元格时,这才会正常工作。

Here is a clean code that addresses your situation这是一个干净的代码,可以解决您的情况

Assume you have two form Form1 and Form2假设您有两个表单Form1Form2

Form 1 has textbox and Button . Form 1textboxButton On Button click Form2 is displayed按钮上单击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;
       }

and in your Form2.cs并在您的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 { }
    } 

And you are done :)你完成了:)

Found correct answer with all your suggestion.根据您的所有建议找到正确答案。

Thank you for your help.感谢您的帮助。

Here is my working code for all needy people.这是我为所有有需要的人提供的工作代码。

In Form1在 Form1 中

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

and in Form2并在 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(); }
        }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM