How to hide GridView Rows with conditions in ASP.NET C#?

Posted by prakashmondal5-21103 under ASP.NET on 3/25/2014 | Points: 10 | Views : 13327 | Status : [Member] | Replies : 3
I want to hide selected day rows in gridview.

Source View:
<td>
<asp:Button ID="Button1" runat="server" Text="Show" Font-Bold="True"
onclick="Button1_Click" />
</td>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
&nbsp;</td>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

<Columns>
<asp:TemplateField HeaderText="Selection">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Days">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("DayName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
Code View( Default.aspx.cs):
protected void Button1_Click(object sender, EventArgs e)
{
String[] arr = new String[6];
arr[0] = "Wednesday";
arr[1] = "Friday";
arr[2] = "Saturday";
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\TestDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
DataTable DT = new DataTable();
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select *from day", con);
da.Fill(DT);
GridView1.DataSource = DT;
GridView1.DataBind();
}
catch (Exception pm)
{

}
finally
{
con.Close();
}
}



But I want to hide those rows which I selected array values.
i.e,
String[] arr = new String[6];
arr[0] = "Wednesday";
arr[1] = "Friday";
arr[2] = "Saturday";

So, only show Monday, Tuesday, Thursday, Sunday rows in gridview.

Please Help Me.




Responses

Posted by: Anant on: 3/28/2014 [Member] Starter | Points: 25

Up
0
Down
on gridview_databound event put code for check your condition (Wednesday,Friday and Saturday)

and according hide rows



prakashmondal5-21103, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: prakashmondal5-21103 on: 3/28/2014 [Member] Starter | Points: 25

Up
0
Down
Mr. Anant,
I know this technique....... But how?????
Please post your answer in details.........

Thanks...
Prakash

prakashmondal5-21103, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/28/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
You can achieve your requirement by using GridView RowDatabound event.
You can use the below code to hide the rows in GridView based on array values.
protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
{
String[] arr = new String[6];
arr[0] = "Wednesday";
arr[1] = "Friday";
arr[2] = "Saturday";

//Loop thorugh the rows in gridview
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Get the Label control here
Label lblctrl = (Label)e.Row.FindControl("Label1");
//Check if array contain the current item text in label
if (arr.Contains(lblctrl.Text))
{
//Hide the Particular row
e.Row.Visible = false;
}
}
}


You can find the complete implementation below

HTML GridView Mark up
<asp:GridView ID="GridView4" runat="server" AutoGenerateColumns="False" 
onrowdatabound="GridView4_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Selection">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Days">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("DayName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


C#:


Load the Data into GridView
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

//Assign the data to GridView
GridView4.DataSource = this.Get_Days();
//Bind the Grid
GridView4.DataBind();

}
}

I have used the below code to populate the dummy records in gridview. You can replace this with your button click method.

//Populate some dummy data
public DataTable Get_Days()
{

DataTable dt = new DataTable();
DataRow dr;
string Item = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday";
string[] list2 = Item.Split(',');
dt.Columns.Add("DayName", typeof(string));
for (int i = 0; i < list2.Length; i++)
{
dr = dt.NewRow();
dr["DayName"] = list2[i];
dt.Rows.Add(dr);
}
return dt;
}


Hiding the rows using GridView RowDataBound event
  protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
{
String[] arr = new String[6];
arr[0] = "Wednesday";
arr[1] = "Friday";
arr[2] = "Saturday";

//Loop thorugh the rows in gridview
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Get the Label control here
Label lblctrl = (Label)e.Row.FindControl("Label1");
//Check if array contain the current item text in label
if (arr.Contains(lblctrl.Text))
{
//Hide the Particular row
e.Row.Visible = false;
}
}
}


Please mark my reply as answer if it answer your question


Thanks,
A2H
My Blog

prakashmondal5-21103, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response