Hiding columns but not page number in grid view asp.net - c#

I am currently working on a grid view. I am using the allow paging method which is working fine and positioned to the right of the grid view.
I want to hide the first column which is working fine except it also removing the paging numbers which stops the user from being able to change page numbers.
Below is the code I am using to hide the column
protected void tblLog_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
The above code hides the correct column but also hides the automatic page numbers created by the grid view allowpaging method.
Thanks for any help you can provide.

Check first to see if it's actually a data row:
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Visible = false;
}

You need to add a condition for datarow and dataheader both
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}

Related

Regarding gridview in asp.net

I am working on a gridview in ASP.Net. I want a particular row to be highlighted on checkbox check so that if any other user logs in the system the row should appear highlighted to them too.
I would suggest create a flag field in your Database table that you bind with the grid view. If you check one row then pass true value to the flag field. Then while you show the Grid view validate like this,
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (Convert.ToString(DataBinder.Eval(e.Row.DataItem, "IsChecked")) == "True")
{
e.Row.BackColor = System.Drawing.Color.Yellow;
}
else
{
e.Row.ForeColor = System.Drawing.Color.Blue;
}
}
}

asp .net gridview , i want the page to appear even if the total pages is one

I am working on an asp .ne project and a have a gridview, According to the users selection the same gridview databounds different amount of data. Sometimes it has 10 pages and sometimes just one. When the pages total is just one then the pager row does not appear, But onRowDataBound event i have an if statement that checks if there is a PagerRow and it successfuly pass it. How can i appear the pager row even if the total pages are just one?.
It passes the following statement or RowDataBound event
if (e.Row.RowType == DataControlRowType.Pager)
{
e.Row.Visible = true;
}
Any help pls?
Override the OnPreRender Event-Handler and then add the following code:
protected void MyGridView_PreRender(object sender, EventArgs e)
{
GridViewRow pagerRow = (GridViewRow) this.BottomPagerRow;
if(pagerRow != null && pagerRow.Visible == false)
pagerRow.Visible = true;
}

Is it Possible to Change the text color for columns in Grid View based upon search item?

Note: i need to change in different columns based upon search request.
Yes you can do it in GridView.RowDataBound event
Like this
protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].BackColor = Color.Beige;
// 0 could be any valid cell index in your row
}
}
for more info Go here

How to highlight specific row in a DetailsView?

I am a new ASP.NET developer. I am using a DetailsView now to dispaly some data from the database. I have the need to highlight certain two rows from the DetailsView. Both rows are VARCHAR data type.
SO HOW TO DO THAT?
Override the databound event and set the e.Row.BackColor = System.Drawing.Color.Red; or what ever color, if you have some logic to execute for finding the row that needs to be highlighted.
protected void detailsView_RowDataBound(object sender,
GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(...)//some condition for selection of row to be higlighted
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
If you need a perticular row, say 4th row to be highlighted and that's fixed then you could directly like this
detailsViewGrid.Rows[3].Row.BackColor = System.Drawing.Color.Red;
The only thing you need to keep in mind that this code would be written in only those events which would come after grid_rowdatabound event(Like pre-render)

Gridview sorting when Header text changed in RowDataBound event

I have a GridView on my webpage whose DataSource is a DataTable which is populated at runtime. AllowSorting property of GridView is True. I have successfully implemented manual sorting for this GridView.
But I had to translate the webpage to other languages for which I used local resource files. I changed the Header text of GridView columns in RowDataBound event. Since then I'm unable to sort the GridView.
protected void GVSummaryTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells.Count > 0)
{
//Translate header text
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = GetLocalResourceObject("GVSummaryTableName").ToString();
e.Row.Cells[1].Text = GetLocalResourceObject("GVSummaryTableLoginLevel").ToString();
e.Row.Cells[2].Text = GetLocalResourceObject("GVSummaryTableLoginID").ToString();
e.Row.Cells[4].Text = GetLocalResourceObject("GVSummaryTableDate").ToString();
}
}
}
What should I do to enable sorting for the columns?
Any help would be appreciated. Thanks!
Changing the code to below solved the problem:
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton LnkHeaderText = e.Row.Cells[1].Controls[0] as LinkButton;
LnkHeaderText.Text = "Name";
}
I am not sure if the problem is related to the Header text since the sorting is normally done with the Sort Expression. Please, make sure you are also giving a value to this property when doing the Sorting. Hope this helps!

Categories