使用下面的代码,我们可以成功显示记录。
要求-我们要到另一列(状态)并显示复选框。可以根据从数据库中获取的值来启用/禁用它。
<asp:GridView ID="HorizontalGrid" runat="server" CellPadding="4" ForeColor="#333333" GridLines="Both" AutoGenerateColumns="false" OnRowDataBound="Horizontal_OnRowDataBound">
<Columns>
<asp:BoundField DataField="Category" HeaderText="Category" HtmlEncode="false" />
<asp:BoundField DataField="Description" HeaderText="Description" HtmlEncode="false" />
</Columns>
</asp:GridView>
这是背后的代码:
SqlConnection con = new SqlConnection("Data Source = .; Initial Catalog = TestDatabase; Integrated Security = TRUE");
SqlCommand cmd = new SqlCommand(Query, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("Category");
dt.Columns.Add("Description");
while (dr.Read())
{
DataRow row = dt.NewRow();
row[0] = dr[0];
row[1] = (string)dr[1];
dt.Rows.Add(row);
}
HorizontalGrid.DataSource = dt;
HorizontalGrid.DataBind();
}