Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
private void txtAdd_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        if (txtAdd.Text != string.Empty && txtSahr.Text != string.Empty)
        {

            DataRow r = dt.NewRow();

            r[0] = txtNameMada.Text;
            r[1] = txtSahr.Text;
            r[2] = txtAdd.Text;
            r[3] = txtAdd_all.Text;

            cleartext();

            dt.Rows.Add(r);
            dataGridView6.DataSource = dt;
            txtsum.Text =
                   (from DataGridViewRow row in dataGridView6.Rows
                    where row.Cells[3].FormattedValue.ToString() != string.Empty
                    select Convert.ToDouble(row.Cells[3].FormattedValue)).Sum().ToString("0,0");

        }
        else
        {
            MessageBox.Show("ادخال كمية من العدد");
            //  txtAdd_Karton.Focus();
            txtAdd.Clear();
            return;
        }
    }
}


What I have tried:

Cannot find column 0.
Posted
Updated 1-Aug-18 1:30am
Comments
Kenneth Haugland 1-Aug-18 5:56am    
Where is dt declared? like dt = new Table?

1 solution

If the error occurs at this line:
C#
r[0] = txtNameMada.Text;
Then the table is empty: it contains no columns, and hence no data.

Since it is a class level variable, I'd start by looking at the method where you load it in the first place, because that establishes the schema for the table - and if you create a local variable called dt within your method:
C#
private DataTable dt = new DataTable();
void MyMethod(void)
   {
   DataTable dt = new DataTable();
   myDataAdapter.Fill(dt);
   ...
   }
Then that masks the class level version and it never gets filled with any schema, much less data.
 
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