Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
Good day

I have created a C# windows application. In the C# windows application I have created a form with a datagridview on it. The data is displayed from a database inside the datagridview.

All the data is displayed from a short proc. It is working 100%.

I want to double click in the datagridview and display the row that I have double clicked in textboxes on a new form.


Any suggestions?

Thanks
Posted

Hello,

I hope it helps:

Following code generates a "Winform" and a number of "TextBox"es dynamically.


C#
private void MyGrid_DoubleClick(object sender, EventArgs e)
{
    if (this.MyGrid.SelectedRows.Count == 0)
          return;

    System.Windows.Forms.Form form = new Form();

    int count = 0;
    foreach (DataGridViewCell cell in this.MyGrid.SelectedRows[0].Cells)
    {
        string value = cell.Value == null ? string.Empty : cell.Value.ToString();

        TextBox textBox = new TextBox()
        {
            Text = value,
            Top = 27 * count + 10
        };

        form.Controls.Add(textBox);

        count++;
    }

    form.Height = (count + 1) * 37;
    form.Show();
}
 
Share this answer
 
v4
Hi JacoBosch,

Follow this steps it may help you.

1. declare a constructor and a public variable in the form which you want to open.
public  string strvalue = string.Empty;
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(string str)
        {
            strvalue = str;
            InitializeComponent();
        }


2.Open form2 like this
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                if (e.ColumnIndex != 1)
                {
                    if (!string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()))
                    {
                        string strValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                        Form2 _Form2 = new Form2(strValue);
                        _Form2.ShowDialog();
                        dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = _Form2.strvalue;
                        dataGridView1.Refresh();
                    }
                }
            }
        }

3. Close form2 on a event and set value in
private void button1_Click(object sender, EventArgs e)
       {
           strvalue = textBox1.Text;
           this.Close();
       }
 
Share this answer
 
hi,i want to fix SOLUTION 2
STEP 3:
private void form2_load (object sender ,EventArgs e)
{
textBox.Text=strvalue;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900