for( int i = 0; i < dataSet.Tables[0].Columns.Count; i++ ) DT.Columns.Add(dataSet.Tables[0].Columns[i]); ============================================================ 你的表里有多少列你就加了多少此
System.Data.SqlClient.SqlDataAdapter ad = new System.Data.SqlClient.SqlDataAdapter(); System.Data.DataSet ds = new System.Data.DataSet(); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); System.Data.DataTable DT = new System.Data.DataTable("table");
DT.Columns.Add(dataSet.Tables[0].Columns[i]);
============================================================
你的表里有多少列你就加了多少此
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
System.Data.DataTable DT = new System.Data.DataTable("table");
cmd.CommandText="select * from tabloc_twohispoints";
cmd.Connection=this.sqlConnection1;
ad.SelectCommand=cmd;
ad.Fill(ds);
int i=1; DT.Columns.Add("ID", System.Type.GetType("System.Int32"));
DT.Columns.Add("PClintID", System.Type.GetType("System.Int32"));
DT.Columns.Add("X1", System.Type.GetType("System.Int32"));
DT.Columns.Add("Y1", System.Type.GetType("System.Int32"));
DT.Columns.Add("Flag", System.Type.GetType("System.Int32"));
DT.Columns.Add("Time", System.Type.GetType("System.DateTime"));
DataRow tmprw;
foreach(DataRow rw in ds.Tables[0].Rows)
{
tmprw = DT.NewRow();
tmprw[0]=i;
tmprw[1]=Convert.ToInt16(rw[0].ToString());
tmprw[2]=Convert.ToInt16(rw[1].ToString());
tmprw[3]=Convert.ToInt16(rw[2].ToString());
tmprw[4]=Convert.ToInt16(rw[3].ToString());
tmprw[5]=Convert.ToDateTime (rw[4].ToString());
//tmprw.AcceptChanges();
DT.Rows.Add(tmprw);
i++; }
this.dataGrid1.DataSource=DT;
我是这样写的,但不是最好的方法,你可以参考kgh0319的方法,是最好的
只要把序号所在的列样式放在第一就可以了,至于该列在DataTable中Columns的什么位置就不重要了.
再说一次,你新加的序号列对于你保存资料没关系,只是在你的界面上显示而已
1 张三 男 ...
2 李四 男
. . .
. . .形成这样的一个结构,进行打印Preview,并打印。