【发布时间】:2016-01-20 17:07:29
【问题描述】:
这个问题被问了很多次,但我似乎找不到直接的答案。
使用 C#,我使用数据表来填充网格视图。我的数据表的第一行包含表标题,所以我循环遍历该行并填充 gridview 标题行。但是现在我需要去掉表格中的第一行(标题行)。我看了一遍,所有消息来源都同意正确的语法是:
strSQL = "SELECT * FROM [Station ID Request$]";
OleDbDataAdapter adaBatch = new OleDbDataAdapter(strSQL, strConnExcel);
DataTable dtBatch = new DataTable();
adaBatch.Fill(dtBatch);
gv_stations.DataSource = dtBatch;
gv_stations.DataBind();
// Fill gridview headers with first row of data (spreadsheet headers)
int i = 0;
foreach (DataColumn col in dtBatch.Columns)
{
gv_stations.HeaderRow.Cells[i].Text = dtBatch.Rows[0][i].ToString();
i++;
}
// Remove the first line of the gridview (contains header info)
gv_stations.DeleteRow(0);
除了最后一行,一切正常,这给了我这个错误:
System.Web.HttpException (0x80004005): The GridView 'gv_stations' fired
event RowDeleting which wasn't handled. at
System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e)
at System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32
rowIndex) at System.Web.UI.WebControls.GridView.DeleteRow(Int32 rowIndex)
我尝试过的每个资源都说最后一行应该没问题。谁能告诉我为什么会出现这个错误?
编辑:这是我的 gridview 对象,根据要求。
<asp:GridView ID="gv_stations" runat="server"
AutoGenerateColumns="True"
CssClass="c_gvv c_gvv_stations_results"
style="white-space:nowrap;">
</asp:GridView>
【问题讨论】:
-
为什么添加标题行只是为了删除它?
-
因为我需要这些信息来填写标题行。
-
那么你想填充gridview的标题行,这就是你想要实现的全部吗?
-
不,我需要用数据表的内容填充gridview。数据表的第一行包含标题信息。所以我需要获取所有行,从第一行填写 gridview 标题,然后从表本身中删除该行。