I have a Gridview that auto generates columns...
The problem is, the data type are all imageurl.
when the gridview populates, the url is in label format.
I could define them as templatefield which works, but the requirement is to auto generate.
I read on MSDN of this, but i dont know how to proceed from there
private void BUChecker_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
}
You can alter this on its TemplateFields. See GridView controls'.
see this for your reference.
I think you use RowDataBound as follows In case you don't want to use template field
protected void RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find your cell suppose it is Cell 0
string imgUrl=e.Row.Cells[0].Text;
Image img = new Imge();
img.ImageUrl =imgUrl;
e.Row.Cells[0].Controls.Add(img);
}
}
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 a WPF application where I need to work on datagrid ,in column header I have added image button to each column which when clicked should pop up with that specific column name and other details.
Currently I have done this:
void data1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
EventManager.RegisterClassHandler(typeof(Button), Button.ClickEvent, new RoutedEventHandler(btnFilterImage_Click) );
}
private void btnFilterImage_Click(object sender, RoutedEventArgs e)
{
viewModel.OnYieldDistinctValues(reportDatagrid.Columns.ToString());
lstYield.ItemsSource = viewModel.TextFilters;
popYield.Placement = PlacementMode.MousePoint;
popYield.IsOpen = true;
}
My question is how can I get the particular column name in btnFilterImage_Click.
Please help.!!
Thanks!!
Cast sender to Button and find its VisualParent using FindVisualParent till you get column.
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
}
}
}
I have a datagridview which is loaded through a strored procedure. It has an ID column which i want to hide it but still access its value. I set the dataKeyNames and i have a method
protected void grdTime_OnDataBound(object sender,EventArgs e)
{
grdTime.Columns[1].Visible = false;
}
which i am trying to hide this column but i get the following error. If i remove the line inside the method it works. Here is the error.
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Try this code in the method and then check..
grdTime.Columns[0].Visible = false;
hide the column in Row_Created event
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].CssClass = "hiddencol";
//e.Row.Cells[2].CssClass = "hiddencol";
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].CssClass = "hiddencol";
//e.Row.Cells[2].CssClass = "hiddencol";
}
}
here hiddencol is a css class
.hiddencol
{
display:none;
}
in the above code i'm hiding second column of my grid
If you've set GridView's AutoGenerateColumns to true(default), the Columns-Collection is empty. Following should work then:
1.)
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
2.)
foreach (TableRow row in GridView1.Controls[0].Controls)
{
row.Cells[0].Visible = false;
}
you can use like this in gridview tag on .aspx page
<Columns>
<asp:BoundField HeaderText="Merchandise Id" DataField="MerchandiseId" Visible="false" /></columns>