开发者

ASP.NET: How to display a different value returned from a database in a GridView

开发者 https://www.devze.com 2023-01-18 10:48 出处:网络
Markup: <asp:GridView ID=\"GridView1\" runat=\"Server\"> <Columns> <asp:BoundField DataField=\"status_column\" HeaderText=\"Status\" />

Markup:

<asp:GridView ID="GridView1" runat="Server">
   <Columns>
      <asp:BoundField DataField="status_column" HeaderText="Status" />
      <asp:BoundField ...
      <asp:BoundField ...
   </Columns>
</asp:GridView>

Code:

GridView1.DataSource = _dataSet
DataBind()

The values stored in my database are integers, 开发者_如何学C1 - 9. Each value has an associated string value that I want displayed in my GridView. Ex: 1 = "Active"; 2 = "On Hold"; etc. How would I filter the incoming data so I can display the associated String value instead of the Integers from my table?


You have two options:

  1. Modify your DataSet, so that it will containt a column for the string representation of the data you want to show.
  2. Use a TemplateField containing a Label. Subscribe to the OnRowDataBound Event of the GridView, and get that Label with e.Row.FindControl("LabelID"), cast the e.Row.DataItem to your DataRow, and assign the string representation of your integer value there.

I would advise against the solution posted by anishmarokey, because the query is not the correct place for this. Think a little big bigger, about localization for instance. Wouldn't work with that solution, but with both of my suggested solutions.

Edit1: Similiar answeres from me regarding the OnRowDataBound Event. If these don't help you, I'm afraid you need to use google to find more examples:
Programmatically access GridView columns and manipulate
Custom GridView delete button
How to bind a complex dictionary to a Gridview in asp.net?

Edit2: I've written an article that elaborates this topic further: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns


instead of doing it in C# , If you are using SQL you can try with

when then condition in a query

SELECT title, price,
        Category = CASE column1
         WHEN column1 = '1' THEN 'Active'
          WHEN column1 = '2' THEN 'On Hold'
          .....
          ELSE 'your value'
        END,
FROM table

which then bind to the gridview


Assuming you are not editing in the gridview, I would just join to the FK table in the SQL and return that as the status instead of the ID.

--Include this as part of your select statement for the datagrid:
SELECT CategoryText as status_column
FROM status_table st INNER JOIN category_table ct ON st.status_column = ct.status_column


Since this is coming back from a database, I think that is is very likely that you will find at table that defines the categories.

All you have to do is join the two

Select Item.Id, Item.Title, Item.Price, Category.Id, Category.Name
From Item
   Inner Join Category
       ON Item.CategoryId = Category.Id


you can save a list of KeyPairValue using Dictionary and try to get the value in the View:

Dictionary<int,string> stringList = //Any code to fill the dictionary, now each integer is related to a string.

in the page

 <asp:BoundField DataField="<%Eval(stringList[status_column])%>" HeaderText="Status" />
0

精彩评论

暂无评论...
验证码 换一张
取 消