C# 如何使用C在gridview中添加新行?
我想从数据库中检索数据,在修改记录之后,我想在Gridview中显示它 我已经通过编辑列创建了列名,这些名称包括日期、位置、职务和详细信息 这是我的asp.net代码C# 如何使用C在gridview中添加新行?,c#,asp.net,gridview,C#,Asp.net,Gridview,我想从数据库中检索数据,在修改记录之后,我想在Gridview中显示它 我已经通过编辑列创建了列名,这些名称包括日期、位置、职务和详细信息 这是我的asp.net代码 <asp:GridView ID="GridViewRecord" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"> <Alternati
<asp:GridView ID="GridViewRecord" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField HeaderText="Date" />
<asp:BoundField HeaderText="Location" />
<asp:BoundField HeaderText="Job Title" />
<asp:BoundField HeaderText="Experience" />
<asp:HyperLinkField HeaderText="Details" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
错误消息:
“System.Data.dll”中发生“System.IndexOutOfRangeException”类型的异常,但未在用户代码中处理
其他信息:找不到列0
我是c的新手,谢谢
if (!this.IsPostBack)
{ DataTable dt = new DataTable();
dt.Columns.Add("0");
dt.Columns.Add("1");
dt.Columns.Add("2");
dt.Columns.Add("3");
dt.Columns.Add("4");
DataRow dr = dt.NewRow();
dr[0] = "12-12-12"; //Error message occured here
dr[1] = "Jeddah";
dr[2] = "Java";
dr[3] = "2";
dr[4] = "View Details";
dt.Rows.Add(dr);
GridViewRecord.DataSource = dt;
GridViewRecord.DataBind();
}
您需要添加到DataTable列,以便将0,1,2,3,4个共同响应处理到表数组中,就像您编写dr[0]=Add0一样
或者,您可以轻松地管理要插入日期的列
dr["2"]= "Java";
您需要添加到DataTable列,以便将0,1,2,3,4个共同响应处理到表数组中,就像您编写dr[0]=Add0一样
或者,您可以轻松地管理要插入日期的列
dr["2"]= "Java";
您没有向DataTable添加任何列,这就是原因
// Create a new DataTable object.
DataTable table = new DataTable();
// Declare a DataColumn
DataColumn column;
// Create the new DataColumn, set DataType, ColumnName and add then add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
table.Columns.Add(column);
// I think that the line-by-line explanation is better for the purpose of this answer
// you can of course do all of this in one row, assuming that you already have a datatable
table.Columns.Add("id", typeof(int));
但是,在您的示例中,您正在创建自己的DataTable,为什么不使用数据库中的DataTable呢?已具有与select查询匹配的列
// create DataSet
DataSet ds = new DataSet();
// your operations for filing the DataSet with data from the database which you have not shared
// ...
// ...
// check to see whether we have a DataTable in the DataSet (if the query fails, ds.Tables.Count == 0)
if (ds.Tables.Count > 0) {
DataRow row = ds.Tables[0].NewRow();
// add data according to the schema
// e.g.
row["id"] = "blah";
// add the rest of the columns
// and lastly add the newly created row to the DataTable;
ds.Tables[0].Rows.Add(row);
}
// now bind
GridViewRecord.DataSource = ds.Tables[0];
GridViewRecord.DataBind();
您没有向DataTable添加任何列,这就是原因
// Create a new DataTable object.
DataTable table = new DataTable();
// Declare a DataColumn
DataColumn column;
// Create the new DataColumn, set DataType, ColumnName and add then add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
table.Columns.Add(column);
// I think that the line-by-line explanation is better for the purpose of this answer
// you can of course do all of this in one row, assuming that you already have a datatable
table.Columns.Add("id", typeof(int));
但是,在您的示例中,您正在创建自己的DataTable,为什么不使用数据库中的DataTable呢?已具有与select查询匹配的列
// create DataSet
DataSet ds = new DataSet();
// your operations for filing the DataSet with data from the database which you have not shared
// ...
// ...
// check to see whether we have a DataTable in the DataSet (if the query fails, ds.Tables.Count == 0)
if (ds.Tables.Count > 0) {
DataRow row = ds.Tables[0].NewRow();
// add data according to the schema
// e.g.
row["id"] = "blah";
// add the rest of the columns
// and lastly add the newly created row to the DataTable;
ds.Tables[0].Rows.Add(row);
}
// now bind
GridViewRecord.DataSource = ds.Tables[0];
GridViewRecord.DataBind();
您还可以使用通用列表或字符串列表,这将使您的代码变得简单 而且,与列表相比,性能方面的DataTable也不好 例如:
您还可以使用通用列表或字符串列表,这将使您的代码变得简单 而且,与列表相比,性能方面的DataTable也不好 例如:
您可以将列的声明、构造函数调用和属性分配压缩到一行中。和System.Type.GetTypeSystem.Int32;应该避免使用typeof关键字。@mason-msdn不同意您的观点。我和他们一起去。仅仅因为他们使用了这个,并不意味着他们不同意我。这只是意味着它是很久以前写的。MSDN文档充满了过时的编码实践。使用typeof运算符不依赖于神奇的字符串。@mason-你可能是对的。但是,我的回答的目的是解释为什么不能将带有列的行添加到没有列的数据表中。是的,我理解这个目的。我只是觉得如果你只做了DataColumn=newdatacolumnid,typeofint,它会更简洁、更简洁;甚至table.Columns.Addid,typeofit;这是您的答案。您可以将列的声明、构造函数调用和属性分配压缩到一行中。和System.Type.GetTypeSystem.Int32;应该避免使用typeof关键字。@mason-msdn不同意您的观点。我和他们一起去。仅仅因为他们使用了这个,并不意味着他们不同意我。这只是意味着它是很久以前写的。MSDN文档充满了过时的编码实践。使用typeof运算符不依赖于神奇的字符串。@mason-你可能是对的。但是,我的回答的目的是解释为什么不能将带有列的行添加到没有列的数据表中。是的,我理解这个目的。我只是觉得如果你只做了DataColumn=newdatacolumnid,typeofint,它会更简洁、更简洁;甚至table.Columns.Addid,typeofit;这是你的答案,你的密码不起作用。而且它没有显示错误。您的代码不工作。以及它没有显示错误。通过此代码,您添加了5行,但他希望添加1行5列。通过此代码,您添加了5行,但他希望添加1行5列。