![](/img/trans.png)
[英]How to display Gridview data based on Dropdown selection that exists on the Gridview itself?
[英]Display gridview row count based on dropdown selection
我正在使用此代碼進行gridview計數並在頁面加載時顯示在標簽中,並且工作正常。
頁面加載:
int rowCount = dtDetails.Rows.Count;
lblTotalRows.Text = rowCount.ToString() + "records found";
我在gridview上方有一個下拉菜單,當我選擇下拉菜單值時,行數必須根據下拉菜單中選擇的值進行更改。
我怎么可能在下拉選中的索引更改中做到這一點
protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtGroup = DataRepository.GetGroup(ddlGroup.Text);
gvDetails.DataSource = dtGroup;
gvDetails.DataBind();
//Now how could I possible show the respective row counts in the label
}
protected void ddlGroup_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dtDept = DataRepository.GetDept(ddlGroup.Text, ddlDept.Text);
gvDetails.DataSource = dtDept;
gvDetails.DataBind();
//Now how could I possible show the respective row counts of both group and
dept row count since they are cascading dropdowns in the label
}
有什么建議么?
我傾向於制作一個SetData()方法,因此所有此類代碼都放在一個地方。 因此,在這種情況下,我將:
protected void SetData(DataTable dtGroup)
{
// Bind the data to the grid
gvDetails.DataSource = dtGroup;
gvODetails.DataBind();
// Show row count
if (!dtGroup.Rows.Count.Equals(0))
lblTotalRows.Text = dtGroup.Rows.Count + " records found";
else
lblTotalRows.Text = "No records found";
}
這樣,您只有一個位置可以完成所有“ bindind”操作,因此在Page_Load中,您可以僅調用此SetData()方法並傳入數據表,而在SelectedIndexChanged上也是如此。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.