我只想知道是否有一个PageIndexChanging
可以控制多个 Gridview ,如附图所示。 Gridview 中的数据绑定来自同一个表格,我决定将其划分为3个网格视图,以便更好地显示数据。我想要一个PageIndexChanging
来控制3个Gridviews。
填写网格视图的C#代码
using (SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["Molecular"].ConnectionString))
{
con.Open();
// Sample Result Mutation Results Details
using (SqlCommand cmd1 = new SqlCommand(@"SELECT *
FROM
MutationResults
WHERE
SampleBranchID=@SampleBranchID ", con))
{
cmd1.Parameters.AddWithValue("@SampleBranchID", lblSampleBranchID.Text);
DataTable dt1 = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
da1.Fill(dt1);
if (dt1 != null && dt1.Rows.Count > 0)
{
GridViewMYD88.DataSource = dt1;
GridViewMYD88.DataBind();
GridViewNOTCH1.DataSource = dt1;
GridViewNOTCH1.DataBind();
GridViewSF3B1.DataSource = dt1;
GridViewSF3B1.DataBind();
}
}
con.Close();
}
答案 0 :(得分:0)
是的,您可以通过将sender
强制转换回GridView来为所有GridView创建一个方法。
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//cast the sender back to a gridview
GridView gv = sender as GridView;
//set the new page index to the gridviw
gv.PageIndex = e.NewPageIndex;
//make sure you rebind the correct data per gridview
gv.DataSource = LoadDataTableFromDB();
//rebind the data
gv.DataBind();
}
public static DataTable LoadDataTableFromDB()
{
string query = "select * from dtabase";
DataTable dt = new DataTable();
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
try
{
adapter.Fill(dt);
}
catch
{
}
}
return dt;
}
您只需确保为每个GridView重新绑定正确的DataSource。如果您只有一个用于所有三个,那么只需重新绑定数据。