根据DataTable列中的代码值显示描述

时间:2017-08-25 14:29:30

标签: c# asp.net gridview datatable

我有一个带有column type和type_amount的DataTable。此DataTable与gridview GridView1绑定。

type type_amount
1       50,000
2       60,000
3       90,000

我要在gridview中显示type_id的描述而不是id的描述,这样我的gridview将显示为

type type_amount
silk        50,000
siffon      60,000
cotton      90,000

我如何在DataTable中执行此操作?

public void Get_Billing_Type()
{
    try
    {
        DataTable dt = new DAL_Set_Billing_Type().Get_Billing_Type();
        foreach (DataRow row in dt.Rows)
        {
            // how to implement logic ??
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    catch (Exception) {  }
}

2 个答案:

答案 0 :(得分:1)

尝试:

            dt.Columns.Add("type_amount", typeof(int));
            foreach (DataRow row in dt.Rows)
            {
                int value = (int)row["type"];

                switch(value)
                {
                    case 1 :
                        row["type_amount"] = 50000;
                        break;
                    case 2 :
                        row["type_amount"] = 60000;
                        break;
                    case 3 :
                        row["type_amount"] = 90000;
                        break;

                }

            }

答案 1 :(得分:0)

我得到了答案。

public void Get_Billing_Type()
{
    try
    {
        DataTable dt = new DAL_Set_Billing_Type().Get_Billing_Type();
        foreach (DataRow row in dt.Rows)
                {
                    switch(row[0].ToString())
                    {
                        case "1":
                            {
                                row["type_id"] = "silk";
                                break;
                            }
                        case "2":
                            {
                                row["type_id"] = "siffon";
                                break;
                            }
                        case "3":
                            {
                                row["type_id"] = "cotton";
                                break;
                            }
                    }
                }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    catch (Exception) {  }
}