日期:2014-05-18  浏览次数:20449 次

给dataset加一个常数列
protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                DataSet   dt   =   SqlHelper.getDataSet( "select   top   10   id,alerttype,accounts   from   AccountAlert ");
                dt.Tables[0].Columns.Add( "myClumn ",   typeof(string),   "stra ");
                GridView1.DataSource   =   dt;
                GridView1.DataBind();
        }
不行。

把dt.Tables[0].Columns.Add( "myClumn ",   typeof(string),   "stra ");
改为dt.Tables[0].Columns.Add( "myClumn ",   typeof(string),   "2 ");
可以为什么,我要加一个值为“stra”列名为“myClumn”的常数列

------解决方案--------------------
DataSet dt = SqlHelper.getDataSet( "select top 10 id,alerttype,accounts from AccountAlert ");
DataColumn newDataColumn = new DataColumn ( "myClumn ",typeof(string));
newDataColumn.AllowDBNull = false;
newDataColumn.DefaultValue = "stra ";
dt.Tables[0].Columns.Add( newDataColumn );
GridView1.DataSource = dt.Tables[0];
GridView1.DataBind();
------解决方案--------------------
public void bind()
{
DataSet ds = new DataSet();
// strConnection = ConfigurationSettings.AppSettings[ "sa "].ToString();
SqlConnection myConnection = new SqlConnection( "server=.;database=Northwind;uid=sa;pwd= ");
SqlDataAdapter myAdapter = new SqlDataAdapter( "SELECT CategoryName, Description FROM Categories ", myConnection);
// 为了分页方便ds是一个全局的变量
myAdapter.Fill(ds);
this.grdTest.DataSource = this.GetRowNumberTable(ds.Tables[0]).DefaultView;
this.grdTest.DataSource = ds.Tables[0].DefaultView;
this.grdTest.DataBind();
}
private DataTable GetRowNumberTable(DataTable dt)
{
DataColumn col = new DataColumn( "RowNumber ", Type.GetType( "System.Int32 "));
dt.Columns.Add(col);
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
if (0 == i)
dt.Rows[i][col] = 1;
else
dt.Rows[i][col] = Convert.ToInt32(dt.Rows[i - 1][col]) + 1;
}
return dt;
}