2013-05-21 54 views
1

我對asp.net.Plz幫助很新。我有一個名爲屬性(模板字段)和值(模板字段)兩欄的gridview。我需要從數據庫表「屬性」綁定屬性列(即,gridview-項目模板(標籤))。屬性表字段是ID和PropertyName。我怎麼能綁定他們?如何將數據庫中的值綁定到gridview?

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> 
+0

顯示你的gridView aspx代碼? –

+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」 & 「屬性名」。

+0

您是否收到任何錯誤?在dt上保留一個斷點並檢查它是否迴流表格行。 – watraplion

+0

沒有錯誤報告。它不顯示gridview –

+0

把一個斷點,並選擇D->右鍵單擊 - >快速觀察 - >看看它的返回錶行或它是空的? – watraplion

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 CODE是完美的只是addde文本= '<%#的eval( 「ID」)%>' 的文本框

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(); 
      } 
     } 

發送烏爾完美的錯誤,那麼你長了,任何異常或警告

相關問題