FILLING DATATABLE FROM GRIDVIEW IN C#

Mark Devid

Total Post:102

Points:714
Posted by  Mark Devid
C# 
C#
 3035  View(s)
Ratings:
Rate this:

How can I fill DataTable from GridView in ASP.NET?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: Filling DataTable from GridView in C#

    Hi Mark,


    I solved like this

      if(myGridView.Rows.Count > 0)
                {
                    var dt= new DataTable();
                    dt.Columns.Add("Column1", typeof(string));
                    dt.Columns.Add("Column2", typeof(Int64));
                    dt.Columns.Add("Column3", typeof(string));
                    foreach (GridViewRow row in gd_endYearSchool.Rows)
                    {
                        var id = row.Cells[1].Text;
             //for find textbox
              var tb1 =row.Cells[2].FindControl("tbNr") as TextBox;
                        int nrord = 0;
                        if(tb1 != null)
                        {
                            var ord = tb1.Text;
                            if (!Int64.TryParse(ord, out nrord))                        {                            nrord = 0;
                            }
                        }
                        var text=row.Cell[3].text;
                        dt.Rows.Add(id,nrord,text);
                    }
                }

      Modified On Apr-04-2018 03:57:29 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!