我创建了一个 dataTable 及其行:
public void method(string ID)
{
sqlConn = new SqlConnection(ConfigurationManager.AppSettings["U"]);
sqlComm = new SqlCommand();
sqlComm.Connection = sqlConn;
DataTable Tablo = new DataTable();
Tablo.Columns.Add(new DataColumn("F1", typeof(string)));
Tablo.Columns.Add(new DataColumn("F2", typeof(string)));
DataRow row;
try
{
sqlComm.CommandText = "Some query...";
sqlComm.Parameters.AddWithValue("@Iden", ID);
sqlConn.Open();
SqlDataReader sqlRead;
sqlRead = sqlComm.ExecuteReader();
while (sqlRead.Read())
{
rowNumber++;
string aim = sqlRead["Aim"].ToString();
row = Tablo.NewRow();
row["F1"] = sqlRead["_ID"].ToString();
row["F2"] = sqlRead["KK_No"].ToString();
Tablo.Rows.Add(row);
}
sqlRead.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlConn.Close();
}
gv.DataSource = Tablo;
gv.DataBind();
}
这是gridview aspx的一面:
<asp:GridView ID="gv" runat="server" Width="100%"
AutoGenerateColumns="False" CssClass="listebaslik"
EnableModelValidation="True" AllowPaging="True"
onpageindexchanging="gv_PageIndexChanging" PageSize="15">
<Columns>
<asp:BoundField DataField="F1" HeaderText="1_No">
<HeaderStyle BackColor="#006699" ForeColor="White" />
<ItemStyle ForeColor="Navy" />
</asp:BoundField>
<asp:BoundField DataField="F2" HeaderText=" 2_No">
<HeaderStyle BackColor="#006699" ForeColor="White" />
<ItemStyle ForeColor="Navy" />
</asp:BoundField>
</Columns>
<PagerStyle ForeColor="#3333CC" />
</asp:GridView>
最后是它的分页部分:
protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvS.PageIndex = e.NewPageIndex;
method(ID);
}
它可以正常工作,但是当单击第二页时,我遇到了这个错误:Conversion failed when converting the nvarchar value '__Page' to data type int
如何解决?谢谢你的回答