Click here to Skip to main content
15,910,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I have created a datagridview at runtime using the createdatagrid() function passing some values and created the keypress event of that datagridview.
In that keypress event I set the selected row to the textbox when user presses the enter key i.e 13.

The above mentioned functionality works fine.

Now I want to make it at class level.

For e.g. I want to pass the textbox5 value to the createdatagrid() or filldata and then display the result back to the textbox5.

Note: I have successfully passed other values but I don't understand how to pass the value of textbox5 and how should I take the result back from function.

Following is the code
Please help me.

Thank you.

C#
public void createdatagrid(int left,int top,int width,int height,)
  {
      dgvnew = new DataGridView();
      dgvnew.Left = left;
      dgvnew.Top = top;
      dgvnew.Size = new System.Drawing.Size(width,height);
      dgvnew.ReadOnly = true;
      dgvnew.RowHeadersWidth = 5;
      dgvnew.ScrollBars = ScrollBars.Vertical;
      this.Controls.Add(dgvnew);
      dgvnew.KeyPress += new KeyPressEventHandler(dgvnew_KeyPress);
  }
  public void filldata(string spname)
  {
      //fill datagirdview
      dsdgv = new DataSet();
      dsdgv = clsobj.getDataset(spname);
      dgvnew.DataSource = dsdgv.Tables[0];
      dgvnew.AutoSize = false;
      for (int i = 0; i < dsdgv.Tables[0].Rows.Count - 1; i++)
          dgvnew.AutoResizeColumn(i);
          dgvnew.Show();
  }
  void dgvnew_KeyPress(object sender, KeyPressEventArgs e)
  {
      if (e.KeyChar == 13)
      {
          textBox5.Text = dgvnew.Rows[dgvnew.CurrentRow.Index - 1].Cells[0].Value.ToString();
          dgvnew.Hide();
      }
  }
  private void textBox5_TextChanged_1(object sender, EventArgs e)
  {
      createdatagrid(550, 172, 300, 200);
      filldata("SP_PC");
  }


[EDIT] Edited for grammar and spelling [/EDIT]
Posted
Updated 9-Sep-10 6:16am
v3
Comments
Dalek Dave 9-Sep-10 9:15am    
Edited for Code Block.

First, creating a new datagridview everytime your textbox's value changes is not such a good idea. Why don't you just recycle the current datagridview. Second,if the datagridview changes the textbox, the textbox will react by changing the datagridview -- this is generally not a good sign. It will end up in infinite triggering of effects.
 
Share this answer
 
hi,
The easiest way is to pass whole your textbox as a parameter to your class.
Another way is to have a delegate in your main form and pass it to your class, something like callback function and change your textbox value in that function.
 
Share this answer
 
Comments
Sagar Khairnar 55 11-Sep-10 3:51am    
i cant decide from where to pass the textbox as parameter...
can u pls help me for that .
pls tell me how can i pass the textbox from which function
aidin Tajadod 13-Sep-10 1:39am    
What Ruselo Rva (Next answer ) said is correct, most of the times the easiest way is not the best way, but for your question by passing TextBox you are actually passing whole object so if you change the Text you do not need to get it back! so add another parameter to your createdatagrid function and pass TextBox5 when you call it.
public void createdatagrid(int left,int top,int width,int height, TextBox oTextBox)
{
//Now you have whole textbox.
}
private void textBox5_TextChanged_1(object sender, EventArgs e)
{
createdatagrid(550, 172, 300, 200,textbox5); // I suppose that textbox5 is an instance of your TextBox
filldata("SP_PC");
}

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