Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have data in gridview from text file .
How to get the data from gridview to Datatable .????
By Looping I can Do that !!!!!
Is there any other way or function to get data in datatable from Gridview without loops?????


:omg: :omg:

My code
C#
private DataTable Get_file(string strFileName) 
    {
                   //Create new DataTable.            
            DataTable table = CreateTable();
            string fileContents=string.Empty;
          //  string[] fileinfo;
            string line;
            DataTable dt =  CreateTable();
            DataRow row;
            try
            {
                string OpenPath = Server.MapPath("..//Attendence_Sheets//" + strFileName.Trim());
                string FILENAME = OpenPath;
                //Get a StreamReader class that can be used to read the file
                StreamReader objStreamReader;
                objStreamReader = System.IO.File.OpenText(FILENAME);

                while ((line =objStreamReader.ReadLine())!=null)
                {
                    //int intLength=30;
                    
                    
                    string emp_att=line.Substring(0,28);
                   // fileinfo = line.Split(contant);
                    row = dt.NewRow();
                   
                            row["MachineId"] = emp_att.Substring(0,2);
                            row["Date"]=emp_att.Substring(2,8);
                            row["Time"]=emp_att.Substring(10,4);
                            row["Status"]=emp_att.Substring(14,4);
                            row["Emp Id"]=int.Parse(emp_att.Substring(18,10));
                   
                    dt.Rows.Add(row);
                    
                
                }


                objStreamReader.Close();
                gvAttendence.DataSource = dt;
                gvAttendence.DataBind();
                return dt;
            }
            finally
            { 
            }
    }

  private int Insert_Monthlyattendence()
    {
        int intResult = 0;
           DataTable dt =new DataTable();

dt=gvAttendence as datatable
if(dt.rows.count>0)
{
}

// Its giving me Error Objecr Reference not set to the instance of an object

    
    }
Posted
Updated 19-Mar-10 1:06am
v3

qasimidl wrote:
Is there any other way or function to get data in datatable from Gridview without loops?????


I don't see why you'd use a loop. Where is your datatable going ? Why do you need one ? There's really no way to answer this question, that I can see. Perhaps if you posted some code and explained what you're trying to do. The most common approach is to store new data in your data source and rebind to it. If your data source is a text file, then yes, you need to write the code to parse it.
 
Share this answer
 
Use DataGrid1.DataSource as DataTable

If you have defined a DataSet/Datatable as a datasource to your Gridview/Datagrid then you don't need to loop through.

Here:
C#
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("IDValue");
dt.Rows.Add("1", "One");
dt.Rows.Add("12", "One2");
dt.Rows.Add("13", "One3");
dt.Rows.Add("14", "One4");
dt.Rows.Add("15", "One5");
DataGrid1.DataSource = dt;
DataGrid1.DataBind();

C#
protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt;
    dt = DataGrid1.DataSource as DataTable;
    // you got datatable of Datagrid in dt now!
}
 
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