poonam tyagi

poonam tyagi

  • NA
  • 5
  • 1.8k

how can fill dataset by gridview in c# .net

Aug 12 2014 10:44 AM
how can fill datatable by gridview with c# i m applying that code but that error occur Input array is longer than the number of columns in this table. my code is that

private void button4_Click(object sender, EventArgs e)
 private void button4_Click(object sender, EventArgs e)
        {

        {

            DataSet d = new DataSet();
            DataTable dt = new DataTable();

            // add the columns to the datatable  
            
            //if (dataGridView4.Rows.Count!=null)
            //{
                for (int i = 0; i < dataGridView4.Rows.Count-1; i++)
                {
                    //for (int j = 0; j < dataGridView4.Columns.Count; i++)
                    //{
                        //for (int i = 0; i < dataGridView4.HeaderRow.Cells.Count; i++)
                        //{
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[0].Selected.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[1].Selected.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[2].Value.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[3].Value.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[13].Value.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[18].Value.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[19].Value.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[21].Value.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[22].Value.ToString());
                        dt.Rows.Add(dataGridView4.Rows[i].Cells[23].Value.ToString());
                        da = new OleDbDataAdapter();
                        da.Fill(d, "dt");
                }

Answers (2)