autocolumn.aspx
来自「asp.net经典案例资料」· ASPX 代码 · 共 59 行
ASPX
59 行
<%@ Page Debug="true" Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>DataGrid Demo- Auto Generate</title>
<script language="C#" runat="server" >
public void Page_Load()
{
//Create Connection object
string dataSource = "Data Source=localhost;";
string security = "user id=sa; password=;";
string initialCatalog = "initial catalog=pubs;";
string cnnString = dataSource + security + initialCatalog;
SqlConnection connection = new SqlConnection(cnnString);
// Create Data Command
string strSql = "select * from [authors]";
SqlCommand command = new SqlCommand(strSql, connection);
// Create Data Adapter
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
// Create DataSet
DataSet dataSet = new DataSet();
try
{
connection.Open();
adapter.Fill(dataSet);
}
catch(SqlException e)
{
Response.Write(e.ToString());
}
finally
{
connection.Close();
}
grid1.DataSource = dataSet.Tables[0];
grid1.DataBind();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>DataGrid Demo- Auto Generate</h3>
<asp:DataGrid id="grid1" runat="server" ShowHeader="true" GridLines="Both" AutoGenerateColumns="true">
<HeaderStyle BackColor="lightblue" Font-Name="Arial" Font-Bold="true" />
<ItemStyle BackColor="lightyellow"/>
</asp:DataGrid>
</form>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?