简体   繁体   中英

Display gridview row count based on dropdown selection

I am using this code to take a gridview count and display in a label on page load and works fine.

Page Load:

int rowCount = dtDetails.Rows.Count;
lblTotalRows.Text = rowCount.ToString() + "records found"; 

I have a dropdown above my gridview and when I select dropdown values the row count have to changed based on the dropdown selected values.

How could I possibly do that in dropdown selected index change

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
}

Any suggestions?

I tend to make a SetData() method so all this kind of code is in one place. So in this instance I would:

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";
 }

This way you only have one place that does all the 'bindind' so in your Page_Load you can just call this SetData() method and pass in the datatable, and the same on your SelectedIndexChanged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM