I hide my columns using the solution in following link
How to hide a TemplateField column in a GridView
However this causes problems with update operations, as gridview acts as hidden rows has null value. So how to hide columns after databind?
protected void begv_OrderDetail_RowCreated(object sender, GridViewRowEventArgs e)
{
((DataControlField)begv_OrderDetail.Columns.Cast<DataControlField>().Where(fld => fld.HeaderText == "FileNo").SingleOrDefault()).Visible = "False";
}
Try this,
grid1.Columns[columnIndex].Visible= false;
Edit based on comment of questioner, for getting values of hidden columns
You can use hidden fields to store column wise values. This article has example that will help how to use hidden fields.
Instead of hiding column you can put the data of columns in datakeynames and later access those values. This will be useful in grabbing how to use DataKeyNames. By this method you may need to pass the id from data key names and get the record.
try this example, (i don't speak english)
into RowDataBound ...
protected void gvTeste_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
teste listaTeste = e.Row.DataItem as ListaTeste;
if (listaTeste.ID == 0)
{
e.Row.Cells[2].Text = "Não Atribuido";
}
if (e.Row.Cells[7].Text == "01/01/0001" || e.Row.Cells[8].Text == "01/01/0001")
{
**e.Row.Visible = false;** // disable row
}
}
}
Related
I have a datatable and it contains a website url and I am displaying entire data in gridview, I want to add hyperlink to all existing urls before binding to gridview.
I am getting data dynamically from database , So I use autogenenerate= true
is it possible ?
You can listen to the OnRowDataBound event and from there trying to infer which cells contain a URL and then, turn them into a link:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
foreach(TableCell cell in e.Row.Cells)
{
if (cell.Text.StartsWith("http"))
{
cell.Text = $"<a href='{cell.Text}'>{cell.Text}</a>";
}
}
}
}
I'm having a problem with onDataRowBound. It seems that it put color on the whole column of cell[0]. The other cells has values on it but still it colors my cell. Here's my code.
protected void GridUserMessage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (String.IsNullOrWhiteSpace(e.Row.Cells[0].Text))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
What's wrong with this?
Add else block where you set cell's color to white (or some other color that your grid has by default)
protected void GridUserMessage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (String.IsNullOrWhiteSpace(e.Row.Cells[0].Text))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.BackColor = System.Drawing.Color.White;
}
}
}
EDIT:
I think CSS styling would be better approach to do this.
In your markup set CSS class to your rows and on RowDataBound event set another class as needed. Something like this:
define "normal" style in markup
<asp:GridView runat="server" RowStyle="normal-row" ...>
and in your RowDataBound method set another class if cell values is empty string, like this:
e.Row.CssClass = "red-row";
You are checking Cell[0] values, what if you are changing in your backend database stored procedure and rearrange column name in select list, for example, you are having
BEFORE
SELECT Id,FirstName,LastName FROM EMPLOYEE
AFTER you/someone else change it to
SELECT Id,LastName,FirstName FROM EMPLOYEE
your code will be easily collapse, so its better to check DataItem of that row for values with name and based on that you can take decision. See code rewritten from your code.
protected void GridUserMessage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Check database column has value or not.
if(String.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(e.Row.DataItem, "ColumnNameOfDataYouWantToCheck")))
{
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}
I have came across the we and check different web pages but I didnt find the one I was looking for. I have a gridview and all I want is to hide one of the rows based on the value in the cell.
What I need to happen is something like in the logic of this :
if (row = "someValue")
{
row.Visible = false;
}
for the record, I have tried this but no luck:
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
string oRoleName = row.Field<string> ("SVal");
if (oRoleName.Equals ("someValue")) {
e.Row.Visible = false;
}
}
It is not base weather the row is the first the 2nd or third (like: e.row[1], e.row[2], etc.) I need to filter the data base on the value in the row. Can anyone teach me how could this be done ?
Would appreciate any help.
In a RowDataBound event add something along the lines of this logic
if (e.Row.Cells[5].Text == "foo") {
e.Row.Visible = false;
}
EDIT:
If youre looking to check the value of each row as its entered (unless im understanding incorrectly, you should probably expand on your question a bit.)
Then you may want to use the event "CellValueChanged"
Check to see if the cell is null beforehand and then do the check for your value and apply logic accordingly below that.
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.CurrentCell != null) {
if (dataGridView1.CurrentCell.Value.ToString() == "foo")
{
// do your stuff here.
}
}
}
What you need to do is get the value of the column within the GridViewRow. You are on the right track. In RowDataBound, find the column you are after. Then check its value.
Here is an example. Use FindControl() to get the control in the specified column for the current row. If the control in that column is a Label, check the text of the label to see if it is the value you want hidden. If so, hide the row.
protected void gv1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = e.Row.FindControl("MyLabel");
if (lbl.Text == "MyValue")
{
e.Row.Visible = false;
}
}
}
I have GridView with 2 columns.
The first column is: test-label (TemplateField)
The second: checkbox (asp:CheckBoxField) that connect to sql table with bit column (done).
I want that on page load - the page will check every row, where the checkbox = true, the test-label.visble will be false.
I know how to write code with SELECT statement to check the value from the SQL table, but don't know how to check every row on the gridview on the page-load.
how can I do that?
(i can't use findcontroll for the checkbox because it's checkboxfield and not just "checkbox".
<asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" />
so, what can I do here? maybe to replace that field with regular cb? (i don't know how to do there databind - on the regular cb).
you can use GridView.RowDataBound Event
so you can do something like
protected void GVRowDataBound(object sender, GridViewRowEventArgs e)
{
var check = (CheckBox) e.Row.FindControl("ID"); // ID is id of the checkbox
var lable = (Label) e.Row.FindControl("LableID");
if(check != null && lable != null)
{
if(check.Checked)
{
lable.Visible = false;
}
}
}
You can't do it in Page.Load because the GridView isn't databound yet.
Try handling GridView.RowDataBound.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("checkbox");
Label lbl = (Label)e.Row.FindControl("test-label");
lbl.Visible = !(cb.Checked);
}
}
I am making conditional formatting changes to the data in my gridview using a RowDataBound event:
void gvReg_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime lastUpdate DateTime.Parse(DataBinder.Eval (e.Row.DataItem, "LAST_UPDATE");
if (lastUpdate < DateTime.Today.AddMonths(-1))
{
Hyperlink hypLastUpdate = (Hyperlink)e.Row.FindControl("hypLastUpdate";
hypLastUpdate.CssClass = "Error";
hypLastUpdate.NavigateUrl = "http://www.someExampleErrorPage.com";
}
}
}
This works, and sets the proper CssClass to the hyperlink (which makes it a jarring shade of bold red), but once the gridview is sorted (via the user clicking a column heading) the css class is reset on hypLastUpdate and it loses both it's style and associated NavigateUrl property.
The control hypLastUpdate is contained in a template field in a gridview, and it's text value is databound to a field called "LAST_UPDATE".
Is this a planned behavior (is sorting supposed to break the conditional formatting done in RowDataBound events?) or is there something I can check to make sure I am not doing something incorrectly?
I am not using the DataBind method anywhere in the code behind, and viewstate is turned on for the gridview in question.
--EDIT--
It ended up being a mistake in event handling.
I was doing:
gvReg.Sorted += {SomeEventHandler}
Inside of the page load event, but only when it wasn't a postback. This function called gvReg.DataBind after the grid view was sorted. I removed the handler wire up and instead added the event handler function to the OnSorted event. I guess assigned delegates to a gridview are not saved in ViewState between callbacks?
Hi here is a quick example of what I meant on my comment. This is the only way i could think of it:
protected void gvReg_Sorting(object sender, GridViewSortEventArgs e)
{
GridView gridView = (GridView)sender;
if (e.SortExpression.Length > 0)
{
foreach (DataControlField field in gridView.Columns)
{
if (field.SortExpression == e.SortExpression)
{
cellIndex = gridView.Columns.IndexOf(field);
break;
}
}
if (pSortExpression != e.SortExpression)
{
pSortDirection = SortDirection.Ascending;
}
else
{
pSortDirection = (pSortDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending);
}
pSortExpression = e.SortExpression;
}
//Retrieve the table from the database
pSortOrder = pSortDirection == SortDirection.Ascending ? "ASC" : "DESC";
List<Partners> partnerList = GetPartnerList();
gvReg.DataSource = partnerList;
gvReg.DataBind();
}