Convert integer to string value on dynamic gridview

I created a gridivew declaratively with linq to sql and it displays my data. However, I want the AccountType and AccountStatus fields to display in a string value. For example AccountType 1 = "Distributor", AccountType 2 = "Manufacturer" but in my database it of course are already set to 1 or 2. I do not want to change the database. 

<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataSourceID="LinqDataSource1">
            <Columns>
                <asp:BoundField DataField="DistributorID" HeaderText="DistributorID" 
                    ReadOnly="True" SortExpression="DistributorID" />
                <asp:BoundField DataField="AccountType" HeaderText="AccountType" 
                    ReadOnly="True" SortExpression="AccountType" />
                <asp:BoundField DataField="AccountStatus" HeaderText="AccountStatus" 
                    ReadOnly="True" SortExpression="AccountStatus" />
                <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" 
                    ReadOnly="True" SortExpression="CompanyName" />
            </Columns>
        </asp:GridView>
        <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
            ContextTypeName="AlternaDB.MarketingDataContext" 
            Select="new (DistributorID, AccountType, AccountStatus, CompanyName)" 
            TableName="Distributors" Where="AccountType == @AccountType">
                    </asp:LinqDataSource>
    
    </div>
    </form>
</body>
 
Please help
 

 

0
kbainey1
11/11/2008 1:39:35 AM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

7 Replies
4367 Views

Similar Articles

[PageSpeed] 32
Get it on Google Play
Get it on Apple App Store

1) create a label template field

2) write on the rowdatabound event of the grid to display 1 or 2 depending on the value of the data

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

//purpose of this function is to add delete confirmation script, add data to dropdown lists and etc.

if (e.Row.RowType == DataControlRowType.DataRow)

{

}

}


Take care,
Selvam

Remember, you can mark more than 1 post as the answer.
0
tdselvam
11/11/2008 2:02:45 AM

Would it be possible for you to show me a full example?

0
kbainey1
11/11/2008 3:28:54 AM

.aspx code: 

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="userid"

DataSourceID="SqlDataSource2" OnRowDataBound="GridView2_RowDataBound">

<Columns>

 

<asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />

<asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Eval("userid")%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

 

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

SelectCommand="SELECT [userid], [firstname], [lastname] FROM [userinfo]"></asp:SqlDataSource>

.cs code

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)

{

if (e.Row.RowType == DataControlRowType.DataRow)

{

Label type = (Label)e.Row.FindControl("Label1");if (type.Text == "1")

{

type.Text =
"Admin";

}

else if (type.Text == "2")

{

type.Text =
"Seller";

}

else if (type.Text == "3")

{

type.Text =
"SalesPerson";

}

}

 

}

 


Regards,
Mahesh
--------------------
click "Mark as Answer" on the post that helps you.
1
maheshshinde9
11/11/2008 6:14:18 AM

Ok thank you but how do I now have kristian and varissa fall under the first and last name columns?

 

        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
            SelectCommand="SELECT [userid], [firstname], [lastname] FROM [userinfo]">
        </asp:SqlDataSource>




    </div>
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="userid"

DataSourceID="SqlDataSource1" OnRowDataBound="GridView2_RowDataBound">

<Columns>

 

<asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />

<asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />

<asp:TemplateField>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text='<%# bind("userid")%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

 



</Columns>

</asp:GridView>


 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            Label type = (Label)e.Row.FindControl("Label1"); if (type.Text == "3")
            {

                type.Text = "Kristian";
            }

            else if (type.Text == "4")
            {

                type.Text = "Varissa";
            }

            else if (type.Text == "5")
            {

                type.Text = "bo";
            }

        }


    }
 
0
kbainey1
11/12/2008 6:10:52 AM

I didn't get last one. What are you trying to do? You said that you have stored AccounType as 1,2,3 etc In Database Table & You want to show like

Admin (if value =1) , Seller (if value =2).

In my above post I have considered userid as type ( which is stored as integer in database) according that I am assing text to Label

means if userid = 1 then Label.Text = "Admin" etc.


Regards,
Mahesh
--------------------
click "Mark as Answer" on the post that helps you.
0
maheshshinde9
11/12/2008 7:44:56 AM

The way I have my example i posted above or the one that you gave me above it shows 3 columns. One column is called Firstname with no data , the second is Lastname with no data , and the thirds has the values of column one and two. I want to put those values in the proper columns.

1
kbainey1
11/13/2008 12:40:17 AM

Hi kbainey1,

You can handle RowDataBound event of GridView:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[1].Text == "1")
                e.Row.Cells[1].Text = "Distributor";
            else if (e.Row.Cells[1].Text == "2")
                e.Row.Cells[1].Text = "Manufacturer";
        }
    }

Thanks,


Qin Dian Tang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Qin
11/13/2008 8:28:03 AM
Reply:

Similar Artilces:

converting web control string values to integers
I have a form using dropdown lists with numbers (1,2,3,4,5) I want to use these numbers to perform calculations but they are returned as stings. How can I convert them into integers? [C#] string MyString = "123"; int x = Int32.Parse(MyString); // throws an exception if MyString does not contain a numeric value such as "123b" [VB.NET] Dim MyString As String = "123" Dim x As Integer = Int32.Parse(MyString) ' throws an exception if MyString does not contain a numeric value such as "123b" Picky thanks much - that worked - was struggling...

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Datagrid(.net 1.1) to GridView(.net 2.0) and using the RowUpdating event how to extract the values from the controls.
Ok  This is the settings: I use a Viewgrid with only itemtemplate colums for example. <asp:TemplateField HeaderText="Test"><ItemTemplate><asp:Label id="LBL_Test" text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' runat="server" meta:resourcekey="LBL_TestResource1"></asp:Label></font></ItemTemplate> <EditItemTemplate><asp:TextBox ID="HDN_Test" Text='<%# DataBinder.Eval(Container.DataItem, "Test1") %>' Runat="server" meta:resourcekey="HDN_TestResource1" /></EditItemTemplate></asp:TemplateField>  The ...

Convert String to Integer for FormView control ??
Hi all, I have this small problem. I have a page which checks for a Role and depending on the role redirects to a different page(with a FormView control)  passing a Querystring that contains an ID. Now when my formview control runs(in edit mode) I get an SQLException error  about 'converting nvarchar ID to data type  int'. I tried something like int32.parse but i still get the same error. Any ideas from all you briliant minds out there? Sounds like a simple problem to solve, but without the actual piece of code, we'd all be guessing.  Can you pos...

How do I... convert string values to a control type?
When I do I get a "Specified cast is not valid. " I have 4 textboxes that use the same Calendar Control. I fill a hidden textbox with the textbox ID from the 4 textboxes. I enable the Calendar Control from a linkbutton depending which text box is selected. When a date is selected from the Calendar Control... it uses the hidden textbox value to get where the textbox ID in which I need to return the selected date value. Thanks! Larry Try something like Page.FindControl(txtHiddenBox.Text).Text =...

Dynamic controls in GridView have no value on PostBack
Hi, I've built a GridView using custom templates.  All the controls used in the GridView are (enabled and hence editable) basic text boxes.  Everything works fine until my custom Save button is clicked and a PostBack event is triggered.  After the user has altered the values of the fields in the GridView, he then clicks Save and I (recursively) scan the different controls in my GridView to save the changes made.  Problem is that the "Text" property of all my TextBox return an empty string!?!?!  Why? Here's my Page_Load:if (!IsPostBack) {DynGridV...

Convert time values from string to integer
 Time stored into database like hh:mm:ss as a string and I need to convert hours, minutes and seconds to integer in order to complete my own calculation at the below. Does any one can help me please.Thank you Dim timeDiff As String            Dim ehours As Integer            Dim eminutes As Integer            Dim eseconds As Integer            ehours = Integer.Parse...

Converting XSD to GridView Formate AND GridView to Xml convertion in.net 2005
hi...           I am new to .net 2005.My problem is        1.I have XSD . in that i am having Entair fields in that               2.i want to convert into Table form.        3.then i want to insert values in that table.        4.Finally i want to convert values in XML formate that's it.. it is very easy in .net 2003.note:-i dont want to use any code.every thing can do with in built ...

Dynamic Gridview with dynamic controls
Hi, I’m trying to insert some dynamic linkbutton inside the rows of a gridview. 1. I First generate the columns inside a datatable. //Create an instance of DataTable DataTable dt = new DataTable(); DataColumn dcol = new DataColumn(”Measure”, typeof(System.String)); dt.Columns.Add(dcol); for (int i = 1; i < 5; i++) { //Create an ID column for adding to the Datatable dcol = new DataColumn(i.ToString(), typeof(LinkButton)); dt.Columns.Add(dcol); } 2.then, I created a row and add it to the datata...

Is there any function to convert string to integer in VB.net?
Is there any function to convert string to integer in VB.net? thank you very much.As Heinlein said "specialization is for insects" explore beyond the universe. Hi There, You can do this Dim Val as String = "50" Dim i As Integer = Convert.ToInt32(Val)DC517Don't forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved. thank you very much dennisAs Heinlein said "specialization is for insects" explore beyond the universe. Hi There, No problem, always welcome...

how to convert string "9999999999" into integer value
Hi I have a string,then max value is "9999999999",10 characters how can i  convert it to integer,and the integer value's type is int32 or int64? I need help Thanks a lot     Your number is too large for an Int32, but you can parse your string into an Int64 like so: Int64 num = Int64.Parse("9999999999");Thanks, EdMicrosoft MVP - ASP/ASP.NET A normal integer(16 bits) cannot hold such value. You should use either long or Int64. It goes like this: Int64 tmpInt1 = Convert.ToInt64("9999999999"); or long tmpInt1 = Convert.ToIn...

How Do I Convert a Dynamic Integer Array Into Comma-delimited String?
Hi. I have the following code but I'm not real happy with it. I think it's really slow doing string concatenation like this. Typically the array will only contain 20 elements, but it's possible to have 400 elements in the array. There HAS to be a better way to do this. Can someone please recommend something? I don't know if it matters, but I'm using plain old Delph 7, so no fancy Delphi XE stuff, please. :) function TFormEquipment.TurnArrayIntoDelimitedString( IntArray: TIntegerDynArray; ctr: integer): string; var TempStr : string; i: integer; begin Temp...

GridView child of GridView
I have a Parent GridView and when you click in the Edit one of the columns has a Template Field which has a Child GridView. When you click Edit on the Child GridView I have a Template Field that has a TextBox. How to I retreive this value of the TextBox? GridViewParent-- GridViewChild (Edit - Template Field containing this GridViewChild)----- TextBox inside Template Field in GridViewChild. Thank you.Peace in ChristMarco Napolihttp://www.ourlovingmother.org What you can do as a solution is to override the RowDataBound and try to find the child GridView control. Once you get a reference to...

How to convert Gridview control to Datalist control
Hi, I have created GridView control with two TemplateFields. Now I want to change GridView Control to DataList Control. Can u please explain how to convert TemplateFields in GridView control to Datalist. Code is...   <asp:GridView ID="grdOptions" runat="server" OnRowDataBound="grdOptions_RowDataBound"    AutoGenerateColumns="false" ShowHeader="False" GridLines="none" CellPadding="0" CellSpacing="0"    Width="100%" OnDataBound="grdOption...

Web resources about - Convert integer to string value on dynamic gridview - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 2/11/2016 3:55:45 PM