asp.net - how to bind values from database to gridview? -
i new asp.net.plz me.
i have gridview 2 columns named-property(template field) , value(template field). need bind property column(ie,gridview- item template(label)) database table 'properties'.properties table fields id , propertyname. how can bind them??
protected void page_load(object sender, eventargs e) { if (!ispostback) { bindview(); } datatable dt1 = new datatable(); sqldataadapter da1 = new sqldataadapter("select id,typename 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 * properties"; con.open(); sqldataadapter da = new sqldataadapter(sql, con); da.fill(dt); gridview1.datasource = dt; gridview1.databind(); con.close(); }
aspx code:
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>
aspx page :
<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 - table column name should "id" & "propertyname".
Comments
Post a Comment