如何从数据库绑定 GridView [英] How to Bind a GridView from database

查看:27
本文介绍了如何从数据库绑定 GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何绑定一个GridView?

How to bind a GridView?

我想在 gridview 中显示我的表格数据.

I want to display my table data in a gridview.

我创建了 SQL 表 EmpDetail,其中包含 ID、Name、Salary Data

I have createed SQL table EmpDetail with columns ID, Name, Salary Data

推荐答案

根据你的场景试试下面的代码

Try below code according to your scenario

希望对你有帮助

protected void GridviewBind ()
{
    using (SqlConnection con = new SqlConnection("Data Source=RapidProgramming;Integrated Security=true;Initial Catalog=RPDB"))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select Name,Salary FROM YOUR TABLE", con);
        SqlDataReader dr = cmd.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        con.Close();
    }
}

<asp:GridView ID="GridView1" runat="server" BackColor="White" 
              BorderColor="#3366CC" BorderStyle="None" 
              BorderWidth="1px" CellPadding="4"
              style="text-align: center; margin-left: 409px" Width="350px">
  <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
  <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
  <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
  <RowStyle BackColor="White" ForeColor="#003399" />
  <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
  <SortedAscendingCellStyle BackColor="#EDF6F6" />
  <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
  <SortedDescendingCellStyle BackColor="#D6DFDF" />
  <SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>;

这篇关于如何从数据库绑定 GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆