C# .NET - copy data from textboxes to data gridview.......plzzzzzzzzzzzzzzz

Asked By ram on 21-Dec-10 09:57 AM
i have four text boxes .i want to transfer data of textboxes in grid view...bt it transfer only one textbox data...whats the problem.. my textboxes are calculting weightages of subjects marks
here is code :
 private void button6_Click(object sender, EventArgs e)
{
 int j = 1; // index
  double x = Convert.ToDouble(Convert.ToDouble(textBox11.Text) * Convert.ToDouble(comboBox1.SelectedItem) / Convert.ToDouble(textBox12.Text));
           

     double y = Convert.ToDouble(Convert.ToDouble(textBox1.Text+ textBox5.Text) / Convert.ToDouble(textBox7.Text)) * Convert.ToDouble(comboBox2.SelectedItem);

  

          double x2 = Convert.ToDouble(Convert.ToDouble(textBox2.Text) * Convert.ToDouble(comboBox4.SelectedItem) / Convert.ToDouble(textBox13.Text));
          
           double x3 = Convert.ToDouble(Convert.ToDouble(textBox4.Text) * Convert.ToDouble(comboBox5.SelectedItem) / Convert.ToDouble(textBox14.Text));
       
           double x4= Convert.ToDouble(Convert.ToDouble(textBox7.Text) * Convert.ToDouble(comboBox6.SelectedItem) / Convert.ToDouble(textBox15.Text));
          
            try
            {
                object[] values = {
                         j,textBox3.Text,x,x3,x4                                   
                                     };
                dt.Rows.Add(values);
                lb.DataSource = dt;
             

                 j = j + 1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
P Patel replied to ram on 21-Dec-10 10:54 AM
You should use list to set the datasource of the gridview as shown below.

  List<double> myList = new List<double>();
    myList.Add(x);
    myList.Add(x2);
    myList.Add(x3);
    myList.Add(x4);
    lb.DataSource = myList;
anil soni replied to ram on 21-Dec-10 11:03 AM
I believe dt is a private DataTable and also promote the int j and make it a private field.
Do not create new instance of dt every time. It should be instanciate only once that is also in the form load.

Put a break point at
lb.DataSource = dt; line and
check wether the datatable contains the newly added row or not and there are multiple row in the datatable.

Also it is not required to set the datasource everytime.

Let me know if this helps you
Anoop S replied to ram on 21-Dec-10 11:06 AM
You can try this for transfer textbox value to datagrid
dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value = textBox1.Text;
dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value = textBox2.Text;
dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value = textBox3.Text;
dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value = textBox4.Text;