简体   繁体   中英

Select a column value of DataGridView in c# winform

I am developing an application in 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. I opened Form2 by:

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

How can I get the selected column value of GDataGridView in the Form1 TextBox?

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

public event Action<string> DatagridCellSelected; 

In your form1 hookup a method with this event.

DatagridCellSelected+=form2_DatagridCellSelected;

In this method do something like this

textbox1.Text = obj;

Now in your form2 handle DataGridView cell enter event

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:

    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

Form 1 has textbox and Button . On Button click Form2 is displayed

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

      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

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

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..!!!!!!!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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