Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,
I am trying to filter the DataGridView rows  based on the given startdate and enddate.
So i am making the rows visible if the date(GridDate) is within the startdate and enddate and making that rows visible in the Grid.
How can i remove/delete the hidden rows from the Grid .i.e if(Visible = false)

Any help will be apprciated.Below is the code I tried


Thanks in Advance
Sharan

What I have tried:

startDate = "2/25/2015"
endDate   = "2/25/2015"
if (startDate <= endDate)// runs foreach loop if startdate and enddate are valid
{
    foreach (DataGridViewRow dr in Grid.Rows)// loops through rows of datagridview
    {
          string deadline = dr.Cells["GridDate"].Value.ToString(); // gets deadline values
          DateTime deadlineRow = Convert.ToDateTime(deadline); // converts deadline string to datetime and stores in deadlineRow variable
          if (startSchedule <= deadlineRow && deadlineRow <= endSchedule) // filters deadlines that are => startDate and <= endDate
          {
              dr.Visible = true; // display filtered rows here.
          }
          else
          {             
             dr.Visible = false; // hide rows that are not beteen start and end date.
             
          }
    }
}
Posted
Updated 28-Feb-17 4:35am

<asp:TemplateField AccessibleHeaderText="GridDate">
                                            <HeaderTemplate>
                                              GridDate
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <%# Eval("LastName")%>
                                            </ItemTemplate>
                                        </asp:TemplateField>
										
										
Use AccessibleHeaderText to find index of this  column then

  Grid.Columns(2).Visible = False
 
Share this answer
 
As you've mentioned DataGridView I'll offer a C# Winforms solution.

First point - if you are using Dates then use a DateTime variable NOT a string. You haven't specified any formatting on the .ToString() and you have an ambiguous format on your startDate and endDate, so just use the proper type.

Second point - it is always far better to filter the data before you put it into the DataGridView e.g. by using a WHERE clause in SQL.

However, this solution assumes that you have already populated the DataGridView and want to remove items from it based on a column "griddate".
C#
var startDate = new DateTime(2017,2,25,0,0,0);
var endDate = new DateTime(2017, 2, 25, 0, 0, 0);

for (var i = dataGridView1.Rows.Count - 1; i >= 0; i--)
{
        if (dataGridView1.Rows[i].Cells["griddate"].Value == null) continue;
        var griddate = DateTime.Parse(dataGridView1.Rows[i].Cells["griddate"].Value.ToString());
        if (griddate < startDate || griddate > endDate)
                dataGridView1.Rows.RemoveAt(i);
}
Note that I start at the end of the collection of rows and work backwards. Imagine I have rows with indexes 1, 2, 3, 4 ... if I start at the beginning and remove Row 2 then Row 4 will no longer exist, I will have row indexes 1, 2, 3. But by starting at the other end I will have already considered row 4 so I don't care if it gets renumbered.

If you have had rows made invisible elsewhere in the code then removing them is even simpler :
C#
for (var i = dataGridView1.Rows.Count - 1; i >= 0; i--)
{
    if (!dataGridView1.Rows[i].Visible)
        dataGridView1.Rows.RemoveAt(i);
}
 
Share this answer
 
dataGridView1.CurrentCell = null;

dataGridView1.Rows[row].Visible = false;
 
Share this answer
 
Comments
manju 3 28-Feb-17 4:22am    
Thanks for the reply.
Tried doing this.But still the rows gets hidden but not deleted.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
     HyperLink hl = (HyperLink) e.row.FindControl("HyperLink1");
       if (hl.Text == "Name1.db")
       {
       e.Row.Visible=false;
       }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900