I am working in ASP.Net, I am saving the status field in Database as true or false. Now, I want to display the true or false as Active or Inactive in the front end in GridView. How to display the data in Gridview.
Thanks in advance.
The alternative is to use your datagrid's RowDataBound event to convert what is it to the strings active / inactive:
Protected Sub gvRequests_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRequests.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lbl As Label = CType(e.Row.FindControl("lblStatus"), Label)
If lbl.Text="1" then
lbl.Text="Active"
else
lbl.Text="Inactive"
end if
end if
end sub
If you're asking how to change true and false to Active and Inactive, you could use a CASE statement in your SQL query, like this:
SELECT CASE Status WHEN 1 THEN 'Active' WHEN 0 THEN 'Inactive' END FROM Something
For a more specific answer, please post more details.
use checkbox column to show the status field. ( set that column to disable )
If you know the cell location of which you want to filter data, you could also do this in the gridviews RowDataBound event.
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[2].Text == "1")
e.Row.Cells[2].Text = "True";
else
e.Row.Cells[2].Text = "False";
}
}
This is what I used to find and replace text in a gridview.
If you are using template field then you can create server side function and call on Eval like below
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblstatus" runat="server" Text='<%# GetStatusText(Eval("status")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
public string GetStatusText(string strIsActive)
{
bool val = Boolean.Parse(strIsActive);
if(val)
{
return "Active";
}
else
{
return "Inactive";
}
}
Related
I have little problem. I'm dynamically populating grid view from the values from the database. I'm trying to remove DropDownList's that don't have any values.
I have this code for now:
if (ddlMyDropDown.Items.Count == 0)
{
ddlMyDropDown = false;
}
else
{
ddlMyDropDown = true;
}
<asp:TemplateField HeaderText="Opis">
<ItemTemplate>
<asp:DropDownList ID="ddlMyDropDown" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
This code works fine but it has one problem. It doesn't remove first ddl in column who is also empty but it removes every other after.
Is there any way to select first ddl who si dinamically loaded in the column and set it to visible false ?
Or some foreach loop that eliminates the ddl's with empty value better ?
Can someone help me ?
Thanks in advance !
You can do this.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl =e.Row.FindControl("ddlMyDropDown") as DropDownList;
if (ddl.Items.Count == 0)
{
ddl.Visible = false;
}
else
{
ddl.Visible = true;
}
}
}
I have GridView to display data as Label
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%# GetIcon((String)Eval("IS_ACTIVE"))%>' SkinID='<%# GetSkinId((String)Eval("IS_ACTIVE"))%>' />
</ItemTemplate>
c#
protected string GetSkinId(string name)
{
if (name == "Y")
{
return "sknActive";
}
else
return "sknInactive";
}
but I get error I can't set SkinID Programmatically, any idea how I can allow SkinID in code behind?
Updated
I decide to not make SkinID, so I'm doing this
<ItemTemplate>
<asp:Label ID="lblIsActive" runat="server" Text='<%# GetIcon((String)Eval("IS_ACTIVE"))%>'
ForeColor='<%# GetColor((String)Eval("IS_ACTIVE"))%>' />
</ItemTemplate>
And my function on c# to get color
protected string GetColor(string name)
{
if (name == "Y")
{
return "#99099";
}
else
return "#03211";
}
I get error that
string can not convert to System.Drawing
The error message is self explanatory. Based on the data source you have the control in your gridview is dynamically getting created and after this it is trying to set the SkinId property and thus the error.
You can achieve this when the row is getting created in your gridview. Yes you can use the RowCreated event of gridvew like this:-
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblIsActive = e.Row.FindControl("lblIsActive") as Label;
if(lblIsActive.Text == "Y")
lblIsActive.SkinID = "sknActive";
else
lblIsActive.SkinID = "sknInactive";
}
}
Please note that this won't work in RowDataBound event since this event is fired after the row is created and when a data row is bound to data.
Update:
First of all your assumption is wrong that we are looping actually we are not. We are simply handling the event which is raised by the gridview control. Anyways since now you have changed your mind and switched to ForeColor approach the problem with your code is that the ForeColor property expects a System.Drawin.Color enum but your passing a string thus the error. For correctint you will have to return Color instead of string like this:-
protected Color GetColor(string name)
{
if (name == "Y")
return Color.Red;
else
return Color.Green;
}
Here I am returning sample colors but you need to replace them with actual intended colors. If you just have the hex string and not sure about the Color enum value then you can use the method mentioned in this answer to do so.
The error message tells you what you need to do: move the initializing of the SkinID property to the Page_PreInit stage of the page lifecycle.
Basically, this entails adding the following event handler to your code behind:
protected void Page_PreInit(object sender, EventArgs e)
{
lblIsActive.SkinId = GetSkinID(IS_ACTIVE); // no need for eval here
}
I have a GridView that has a column called Active which shows either a 1 (for active) or a -1 for inactive.
However as this is being implemented into a front end UI I do not want users to be presented with what seems to them as useless integers, however a Active or Not active to be presented in the GridView on Page_Load.
The code would look something like -
protected void Page_Load(object sender, EventArgs e)
{
//code here to modify the column 'Active' in the GridView
//GridView ID="GV1"
if (row.Cells[1].Text == "1")
{
row.Cells[1].Text = "Active";
}
if (row.Cells[1].Text == "-1")
{
row.Cells[1].Text = "Not Active";
}
}
And the column in the GridView is -
<asp:BoundField HeaderText="Active" DataField="Active" SortExpression="Active"></asp:BoundField>
How can I do this as I do not want to edit the database in order for the UI to be more presentable?
Try the OnRowDataBound Gridview event.
Question pretty much says it all. On my aspx page I have a GridView and under Columns I have a bunch of BoundFields, one of which is a TemplateField
<asp:TemplateField HeaderText = "Status">
<ItemTemplate>
<asp:HyperLink ID = "HyperLink1" runat = "server" Target = "_blank"
NavigateUrl = '<%# Eval("URL") %>'
Text = '<%#Eval("Status") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Now, I want this Hyperlink to map to a different image, depending on what the text is evaluated to. For example, 'Success' displays a big ol' smiley face instead, 'Failed' displays a frowney face, and so on. How can I achieve this?
Thanks for looking.
You can put an image in the hyperlink like
<img src='/images/status/<%#Eval("Status") %>.jpg' />
and just make a different image for each status by name. Otherwise you'll probably have to do something on the DataBind event.
Try this
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink HyperLink1 = e.Row.FindControl("HyperLink1");
if(SomeText == "Success")
HyperLink1.NavigateUrl = "Url to Smiley";
else
HyperLink1.NavigateUrl = "Url to Frowney";
}
}
HyperLink HyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
switch (HyperLink1.Text)
{
case "Completed":
HyperLink1.ImageUrl = "Images\\Success.png";
HyperLink1.ToolTip = "Completed";
etc
The ToolTip property maps to the alternate text for the image.
Thanks to codingbiz for getting me started.
If you are trying to set the ImageUrl property I suggest using the RowDataBound event. The handler method could would look something like:
protected void questionsGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
DataSourceDataType row;
HyperLink hyperLink1;
if (e.Row.RowType == DataControlRowType.DataRow & e.Row.DataItem is DataSourceDataType)
{
row = (DataSourceDataType)e.Row.DataItem;
hyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
hyperLink1.ImageUrl = (row.IsSuccess) ? "~/images/success.png" : "~/images/failure.png";
}
}
Another trick I have used is altering the data object you are binding to to have a property which indicates the URL to use:
partial class DataSourceDataType
{
public string SuccessImgURL
{
get
{
return (IsSuccess) ? "~/images/success.png" : "~/images/failure.png";
}
}
}
Then you bind to that property.
Note: IsSuccess would need to be replaced with your own field name or boolean condition.
I often use this with LINQ to SQL objects, so adding properties can be done in a separate file using partial classes. This way you do not have to worry about the LINQ to SQL tools removing your additions.
I am creating a gridView that allows adding new rows by adding the controls necessary for the insert into the FooterTemplate, but when the ObjectDataSource has no records, I add a dummy row as the FooterTemplate is only displayed when there is data.
How can I hide this dummy row? I have tried setting e.row.visible = false on RowDataBound but the row is still visible.
You could handle the gridview's databound event and hide the dummy row. (Don't forget to assign the event property in the aspx code):
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 1)
GridView1.Rows[0].Visible = false;
}
Please try the following
protected void GridView1_DataBound(object sender, EventArgs e)
{
GridView1.Rows[0].Visible = false;
}
I think this is what you need:
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false" ShowFooter="true" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="headertext">
<ItemTemplate>
itemtext
</ItemTemplate>
<FooterTemplate>
insert controls
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and the codebehind:
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["style"] = "display:none";
}
}
But I do not understand why you are adding your "insert controls" to the footer instead of placing them below the grid.
Maybe try:
e.Row.Height = Unit.Pixel(0);
This isnt the right answer but it might work in the meantime until you get the right answer.
Maybe use CSS to set display none?!
This is the incorrect usage of the GridView control. The GridView control has a special InsertRow which is where your controls should go.
GridView has a special property to access Footer Row, named "FooterRow"
Then, you cold try yourGrid.FooterRow.Visible = false;
I did this on a previous job, but since you can add rows, I always had it visible in the footer row. To make it so that the grid shows up, I bound an empty row of the type that is normally bound
dim row as Datarow = table.NewRow()
table.AddRow(row)
gridView.DataSource = table
gridView.Databind()
then it has all the columns and then you need. You can access the footer by pulling this:
'this will get the footer no matter how many rows there are in the grid.
Dim footer as Control = gridView.Controls(0).Controls(gridView.Controls(0).Controls.Count -1)
then to access any of the controls in the footer you would go and do a:
Dim cntl as Control = footer.FindControl(<Insert Control Name Here>)
I'd assume you'd be able to do a:
footer.Visible = false
to make the footer row invisible.
I hope this helps!
Edit I just figured out what you said. I basically delete the row when I add a new one, but to do this you need to check to see if there are any other rows, and if there are, check to see if there are values in it.
To delete the dummy row do something like this:
If mTable.Rows.Count = 1 AndAlso mTable.Rows(0)(<first column to check for null value>) Is DBNull.Value AndAlso mTable.Rows(0)(<second column>) Is DBNull.Value AndAlso mTable.Rows(0)(<thrid column>) Is DBNull.Value Then
mTable.Rows.Remove(mTable.Rows(0))
End If
mTable.Rows.Add(row)
gridView.Datasource = mTable
gridView.Databind()
Why are you not using the EmptyDataTemplate? It seems to work great even though I have only been using it for a couple days...
You should use DataKeyNames in your GridView:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="FooID">
And then retrieve it on your code:
GridView1.DataKeys[0].Value.ToString()
Where "0" is the number of the row you want to get the "FooID"
To make it visible, just use:
Gridview.Rows.Item(i).Attributes.Add("style", "display:block")
And to make it invisible
Gridview.Rows.Item(i).Attributes.Add("style", "display:none")
If you do not want to display data when the column/row is null:
if (!String.IsNullOrEmpty(item.DataName))
{
e.Row.Visible = false;
}
It can easily be done by SQL
USE YourdatabaseName select * from TableName where Column_Name <> ''