如何将数据库中的值绑定到gridview?

时间:2013-05-21 06:03:49

标签: asp.net

我是asp.net.Plz的新手帮助我。

我有一个gridview,其中有两列名为-properties(模板字段)和value(模板字段)。我需要从数据库表'properties'绑定属性列(即gridview- item模板(标签)).Properties表字段是ID和PropertyName。我该如何绑定它们?

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindView();


            }

                DataTable dt1 = new DataTable();
                SqlDataAdapter da1 = new SqlDataAdapter("select ID,TypeName from ProductTypes", con);
                da1.Fill(dt1);
                DropDownList1.DataSource = dt1;
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataTextField = "TypeName";
                DropDownList1.DataBind();
            }

        public void BindView()
        {

            DataTable dt = new DataTable();
            string sql = "select * from Properties";
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
            con.Close();
        }

aspx代码:

 asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Horizontal" 
    style="z-index: 1; left: 52px; top: 230px; position: absolute; height: 133px; width: 344px">
    <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
    <Columns>
        <asp:TemplateField></asp:TemplateField>
        <asp:TemplateField HeaderText="Property">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("PropertyName") %>' ></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Value">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
    <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
    <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
    <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
    <AlternatingRowStyle BackColor="#F7F7F7" />
</asp:GridView>

2 个答案:

答案 0 :(得分:0)

aspx页面:

<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
        <Columns>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="lblID" Text='<%#Bind("ID") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Property Name">
                <ItemTemplate>
                    <asp:Label ID="lblPropertyName" Text='<%#Bind("PropertyName") %>' runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

dt - 表格列名称应为“ID”&amp; “属性名”。

答案 1 :(得分:0)

我认为您的代码没有任何问题

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
        CellPadding="3" GridLines="Horizontal" 
        style="z-index: 1; left: 52px; top: 230px; position: absolute; height: 133px; width: 344px">
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <Columns>
            <asp:TemplateField></asp:TemplateField>
            <asp:TemplateField HeaderText="Property">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("PropertyName") %>' ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Value">
                <ItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("id") %>' ></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <AlternatingRowStyle BackColor="#F7F7F7" />
    </asp:GridView>

ASPX代码非常完美,只需添加文字='&lt;%#Eval(“id”)%&gt;'对于文本框

ASPX.CS代码也没有问题我没有改变任何东西及其工作f9

     protected void Page_Load(object sender, EventArgs e)
            {

                if (!IsPostBack)
                {
                    BindView();


                }

                //DataTable dt1 = new DataTable();
                //SqlDataAdapter da1 = new SqlDataAdapter("select ID,TypeName from ProductTypes", con);
                //da1.Fill(dt1);
                //DropDownList1.DataSource = dt1;
                //DropDownList1.DataValueField = "ID";
                //DropDownList1.DataTextField = "TypeName";
                //DropDownList1.DataBind();

            }
            public void BindView()
            {

                DataTable dt = new DataTable();
                string sql = "select * from Properties";
                //con.Open();
                //SqlDataAdapter da = new SqlDataAdapter(sql, con);

                dt.Columns.Add("ID",typeof(int));
                dt.Columns.Add("PROPERTYNAME",typeof(string));

                dt.Rows.Add(25, "propety1");
                dt.Rows.Add(50, "property2");
                dt.Rows.Add(10, "propety3");

               // da.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
                //con.Close();
            }
        }

发送你的完美错误你得到的,任何例外或警告