显示单列的不同列值

时间:2012-02-23 06:05:46

标签: c# asp.net gridview

我想将单列值显示给gridview不同的列..

例如:              我的专栏价值是“休假:12-医疗假:13年假:03”.. 我想拆分上面的字符串值,并在网格列中显示上述值,如...

 Employee Id   Employee Name  Casual Leave     Medical Leave    Annual Leave

 00624323      James          12               13               03
 00624324      Fernando       12               14               05

注意:员工ID和员工姓名是不同的列

我的部分代码在这里:

DataSet5TableAdapters.sp_getallempleaveTableAdapter TA = new    DataSet5TableAdapters.sp_getallempleaveTableAdapter();
DataSet5.sp_getallempleaveDataTable DS = TA.GetData();
        if (DS.Rows.Count > 0)
        {
            DataView datavw = new DataView();
            datavw = DS.DefaultView;
            datavw.RowFilter = "fldempid='" + txtempid.Text + "' and fldempname='" + txtempname.Text + "'";
            if (datavw.Count > 0)
            {
                string leavehistory = Convert.ToString(datavw[0]["fldleavehistory"]);
                string[] textarray = leavehistory.Split('-');
                foreach (string samtext in textarray)
                {
                    if (samtext.StartsWith(leavehistory))// I want to check the string with array value
                    {
                        string newtext = samtext.Split(':')[1];
                    }
                }

            }
        }
我怎么办?请帮我解决这个问题

2 个答案:

答案 0 :(得分:0)

在数据库中使用这样的复合列永远不是一个好主意。你为什么不把列拆成3列(CasualLeave,MedicalLeave和AnnualLeave),每列都包含一个数字?

答案 1 :(得分:0)

使用leavehistory和newtext,你可以创建一个数据表并将其绑定到gridview,或者你可以改变你的sp直接返回splitit结果。

因为你在客户端编码了这么多,所以现在更好地创建一个新的数据表并将行提供给它并将这个数据表添加到gridsource

编辑: -

示例: -

DataTable newdatatable = new DataTable("New");
newdatatable.Columns.Add("Employee Id");
newdatatable.Columns.Add("Employee Name");
newdatatable.Columns.Add("Casual Leave");
newdatatable.Columns.Add("Medical Leave");    
newdatatable.Columns.Add("Annual Leave");
newdatatable.AcceptChanges();
DataSet5TableAdapters.sp_getallempleaveTableAdapter TA = new    DataSet5TableAdapters.sp_getallempleaveTableAdapter();    
DataSet5.sp_getallempleaveDataTable DS = TA.GetData();
if (DS.Rows.Count > 0)
{
      DataView datavw = new DataView();
      datavw = DS.DefaultView;
      datavw.RowFilter = "fldempid='" + txtempid.Text + "' and fldempname='" + txtempname.Text + "'";
      if (datavw.Count > 0)
      {
          string leavehistory = Convert.ToString(datavw[0]["fldleavehistory"]);
            string[] textarray = leavehistory.Split('-');DataRow drow = newdatatable.NewRow();
                    drow[0]=txtempid.Text;
                    drow[1]=txtempname.Text;
            foreach (string samtext in textarray)
            {
                if (samtext.StartsWith(leavehistory))// I want to check the string with array value
                {                        
                    drow[samtext.Split(':')[0]]=samtext.Split(':')[1];
                }
            }
            newdatatable.Rows.Add(drow);
            newdatatable.AcceptChanges();
        }
}
gridview1.DataSource=newdatatable;