从gidview获取数据到Datatable [英] get data from gidview to Datatable

查看:56
本文介绍了从gidview获取数据到Datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从文本文件在gridview中有数据.
如何将数据从gridview获取到Datatable.????
通过循环我可以做到!!!!!
还有其他方法或功能可以从Gridview的数据表中获取数据而无循环吗???


:omg::omg​​:

我的代码

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

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

    
    }

推荐答案

qasimidl写道:
qasimidl wrote:

还有其他方法吗?或函数从Gridview中获取数据表中的数据而没有循环??????

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.


使用 DataGrid1.DataSource作为DataTable

如果您已将DataSet/Datatable定义为Gridview/Datagrid的数据源,则无需遍历.

此处:
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:
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();


protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt;
    dt = DataGrid1.DataSource as DataTable;
    // you got datatable of Datagrid in dt now!
}


这篇关于从gidview获取数据到Datatable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆