当前位置:  开发笔记 > 编程语言 > 正文

运行时错误'找不到列0'

如何解决《运行时错误'找不到列0'》经验,为你挑选了1个好方法。

运行时错误找不到列0.下面是我的代码

string connectiostring = (string)ConfigurationSettings.AppSettings["NorthwindConnectionString"];
    SqlConnection conn = new SqlConnection(connectiostring);
    SqlCommand cmd = new SqlCommand("select * from Employees", conn);
    conn.Open();
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
    DataSet data = new DataSet();
    adapter.Fill(data,"Employees");
    data.Tables["Employees"].Columns.Add("Testcolumn");
    DataTable t1 = new DataTable("Employees");
    DataRow newrow = t1.NewRow();
    newrow[0] = "10";\\this the line i am getting error
    newrow[1] = "Pradeep";
    newrow[2] = "Kumar";
    data.Tables["Employees"].Rows.Add(newrow);
    GridView2.DataSource = data;
    GridView2.DataBind();

请帮帮我谢谢,



1> ChrisF..:

您尚未将列添加到DataTable.

t1.Columns.Add(new DataColumn
                   {
                       DataType = string,
                       ColumnName = "First Name"
                   });

对每个列重复此操作,为每个列提供正确的类型.

推荐阅读
sx-March23
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有