gpt4 book ai didi

c# - 如何使用 C# 在 gridview 中添加新行?

转载 作者:太空宇宙 更新时间:2023-11-03 17:05:10 25 4
gpt4 key购买 nike

我想从数据库中检索数据,修改记录后我想在 Gridview 中显示它。

我已经通过 Edit columns 创建了列名称,这些名称是 Date,Location,Job Title,Details

这是我的asp.net代码

<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>

然后我尝试直接添加一条示例记录。但是我收到一个错误。这是我在页面加载时的 C# 代码

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();

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();
}
}

错误信息:

An exception of type 'System.IndexOutOfRangeException' occurred in` System.Data.dll but was not handled in user code
Additional information: Cannot find column 0.

我是c#新手,谢谢

最佳答案

您尚未向 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,为什么不使用数据库中的数据表?那将已经有与您的选择查询匹配的列。

// 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();

关于c# - 如何使用 C# 在 gridview 中添加新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39595345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com