如何在数据集的数据表中拆分列的值?

时间:2012-08-30 12:59:12

标签: c# asp.net

在我的数据集中,我有很多行,

In first row last column has the value is "12356.56@Firefox 1@23423"
In Second row same column has the value is "12356.56@Chrome2.0@23423"
In Third row same column has the value is "3423@Firefox 14.0@sdfsd"

此处不是像上面那样显示相同的值..

I need to display just "Firefox" if that column has Firefox in UI

I need to display just "Chrome" if that column has Chromein UI

我该怎么做......

3 个答案:

答案 0 :(得分:1)

使用此辅助功能

public static string GetBrowser(string str)
{
    str = str.ToUpper();
    if(str.Contains("CHROME"))
    {
        return "Chrome";
    } 
    else if(str.Contains("FIREFOX")) 
    {
        return "Firefox";
    }
    return "Unknown";
}

然后,当您将数据集绑定到UI时,使用它来显示值。

答案 1 :(得分:1)

如果您有要查看的内容列表,可以使用.Contains进行检查:

var myValue = myDS[rowIndex][lastColumn].ToString();
if (myValue.Contains("Firefox"))
    return "Firefox";
else if (myValue.Contains("Chrome"))
    return "Chrome";
else
    return "Unknown";

演示:http://ideone.com/pMVeD


从您给出的示例中可以看出,以下逻辑可能有效,但应针对更多样本值进行测试:

var myValue = myDS[rowIndex][lastColumn].ToString();
var parts = myValue.Split('@');
var browser = parts[1].Split(' ','0','1','2','3','4','5','6','7','8','9');
return browser[0];

演示:http://ideone.com/NrY1e

答案 2 :(得分:1)

这是代码。这可能有所帮助。

        DataSet ds = new DataSet();
        //DataTable dt = new DataTable();
        ds.Tables.Add("table0");
        DataColumn dc = new DataColumn("browser");
        ds.Tables[0].Columns.Add(dc);
        ds.Tables[0].Rows.Add("12356.56@Firefox1@23423");
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string str = ds.Tables[0].Rows[i][0].ToString();
            if((str.ToLower().CompareTo("firefox"))!=0)
            {
                ds.Tables[0].Rows[i][0] = "firefox";
            }
        }
        _gridView.DataSource = ds;
        _gridView.DataBind();