I want to hide selected day rows in gridview.
database table:
Design View (Default2.aspx):
Source View:
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();
}
}
{
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();
}
}
Output:
But I want to hide those rows which I selected pink background array values.
i.e,
String[] arr = new String[6];
arr[0] = "Wednesday";
arr[1] = "Friday";
arr[2] = "Saturday";
arr[0] = "Wednesday";
arr[1] = "Friday";
arr[2] = "Saturday";
So, only show Monday, Tuesday, Thursday, Sunday rows in gridview.
Please Help Me.
5 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Prakash MondalPosted Mar 26, 2014, 3:01 AM
Prakash MondalPosted Mar 26, 2014, 2:32 AM
Jignesh TrivediPosted Mar 26, 2014, 1:12 AM
hi,
you can also use Linq for same
try....
DataTable dt = new DataTable();
dt.Columns.Add("Day");
dt.Columns.Add("Time");
dt.Rows.Add(new object[] { "MonDay", "" });
dt.Rows.Add(new object[] { "TuesDay", "" });
dt.Rows.Add(new object[] { "Wednesday", "" });
dt.Rows.Add(new object[] { "Thursday", "" });
dt.Rows.Add(new object[] { "Friday", "" });
dt.Rows.Add(new object[] { "Saturday", "" });
dt.Rows.Add(new object[] { "Sunday", "" });
String[] arr = new String[6];
arr[0] = "Wednesday";
arr[1] = "Friday";
arr[2] = "Saturday";
var newDt = (from d in dt.AsEnumerable()("Day"))
where !arr.Contains(d.Field
select d);
GridView1.DataSource = newDt;
GridView1.DataBind();
hope this will help you.
Abhay ShankerPosted Mar 26, 2014, 1:07 AM
protected void custGV_SelectedIndexChanged(object sender, EventArgs e)
{
custGV.Rows[custGV.SelectedIndex].Visible = false;
}
Munesh SharmaPosted Mar 26, 2014, 12:47 AM
In the page_load, after you bind to the GridView successfully, please do the following things:
protected void Page_Load(....)
{
if(!IsPostBack)
{
foreach(GridViewRow r in GridView.Rows)
{
r.Visible = r.Cells[2].Text !="0";
//Or using ItemTemplate:
r.Visible = (r.FindControl("id of label in column3") as Label).Text != "0";
}
}
}