在DataGrid中构造自增值列....
这里有三种方法,大家可以参考!
注: aspx代码部分都用如下代码。
=============================================
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 56px; POSITION: absolute; TOP: 72px" runat="server"
Width="520px" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White"
CellPadding="4" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
<Columns>
<asp:TemplateColumn HeaderText="UseDataSetIndex">
<ItemTemplate>
<%#Container.DataSetIndex + 1 %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="customid" DataField="customID"></asp:BoundColumn>
<asp:BoundColumn HeaderText="name" DataField="name"></asp:BoundColumn>
</Columns>
</asp:datagrid>
<br>
<br>
<asp:button id="btnSearch" style="Z-INDEX: 100; LEFT: 96px; POSITION: absolute; TOP: 448px"
runat="server" Text="Button"></asp:button>
</form>
</body>
</HTML>
=============================================
下面是具体的方法:
=============================================
(一)临时表法
private void GetData()
{
SqlConnection cnn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlServer"]);
SqlCommand cmd=new SqlCommand("select identity(int,1,1) as customID,* into #t from sysusers;select * from #t",cnn);
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
try
{
adapter.Fill(ds);
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
(二)构造自增值列
private void GetDataByAddingColumn()
{
SqlConnection cnn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["SqlServer"]);
SqlCommand cmd=new SqlCommand("select* from sysusers",cnn);
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
DataTable dt=new DataTable("tmpTable");
DataColumn dc=new DataColumn("customID",typeof(System.Int32));
dc.AutoIncrement=true;
dc.AutoIncrementSeed=1;
dc.AutoIncrementStep=1;
dt.Columns.Add(dc);
try
{
adapter.Fill(dt);
DataGrid1.DataSource=dt.DefaultView;
DataGrid1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
这里你就需要注意操作顺序了,要先构造表,然后再调用adapter.Fill(dt),否则你什么也可不到别骂我,骂自己是猪头吧!^_^
(三)绑定时动态构建自增值显示列
<asp:TemplateColumn HeaderText="UseDataSetIndex">
<ItemTemplate>
<%#Container.DataSetIndex + 1 %>
</ItemTemplate>
</asp:TemplateColumn>
也许你看到这里会毫不犹豫地选择第三种方法,动动自己的脑子,前两种方法也是有用武之地D !
===========================================
cs里调用函数的部分代码是这样的:
在这个例程中大家可以测试第一种和第三种方案。
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
GetData();
}
}
在这个例程中大家可以测试第二种和第三种方案。
private void Button1_Click(object sender, System.EventArgs e)
{
GetDataByAddingColumn();
}