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

markup:

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

The code:

GridView1.DataSource = _dataSet
DataBind()

The values ​​stored in my database are integers, 1 - 9. Each value has an associated string value that I want to display in my GridView. Example: 1 = "Active"; 2 = "On"; etc. How can I filter the input so that I can display the associated String value instead of the integers from my table?

+3
source share
5 answers

You have two options:

  • Modify your DataSet so that it contains a column for the row representing the data you want to show.
  • TemplateField . OnRowDataBound GridView, e.Row.FindControl( "LabelID" ), e.Row.DataItem DataRow, .

, anishmarokey, . , . , .

Edit1: OnRowDataBound. , , Google, :
GridView


GridView
Gridview asp.net?

Edit2: , : http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns

+2

, #. SQL,

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

gridview

+1

, gridview, FK SQL 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
+1

, , , , .

, ,

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

you can save the KeyPairValue list with a 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.

On the page

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

Source: https://habr.com/ru/post/1767006/


All Articles