根据下拉选择显示gridview行计数

时间:2014-04-04 10:37:31

标签: c# asp.net .net gridview

我正在使用此代码获取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
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我倾向于制作一个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上传递相同的内容。