c# - How to bind grid in ASP.NET? -
i cant bind grid. dont know doing wrong, grid appears empty when run program.
here code ::
protected void page_load(object sender, eventargs e) { if (!ispostback) this.bindgrid(this.gridview1); } private void bindgrid(gridview grid) { sqlcommand cmd = new sqlcommand("select * person", cn); sqldataadapter da = new sqldataadapter(cmd); datatable dt = new datatable(); da.fill(dt); grid.datasource = dt; grid.databind(); } <body> <form id="form1" runat="server"> <div style="margin-left: 240px"> <asp:gridview id="gridview1" runat="server" cellpadding="4" forecolor="#333333" gridlines="none" width="856px" autogeneratecolumns = "false" showfooter = "true" showheader="true" borderstyle="groove" captionalign="top" horizontalalign="center" onrowdatabound="gridview1_rowdatabound" > <rowstyle backcolor="#f7f6f3" forecolor="#333333" /> <columns> <asp:boundfield headertext="id" /> <asp:boundfield headertext="first name" /> <asp:boundfield headertext="last name" /> <asp:boundfield headertext="home phone #" /> <asp:boundfield headertext="cell #" /> <asp:boundfield headertext="email address" /> <asp:boundfield headertext="nic #" /> <asp:templatefield headertext="action"> <itemtemplate> <asp:button id="button1" runat="server" text="button" /> <asp:button id="button2" runat="server" text="button" /> </itemtemplate> </asp:templatefield> </columns> <footerstyle backcolor="#5d7b9d" font-bold="true" forecolor="white" /> <pagerstyle backcolor="#284775" forecolor="white" horizontalalign="center" /> <selectedrowstyle backcolor="#e2ded6" font-bold="true" forecolor="#333333" /> <headerstyle backcolor="#5d7b9d" font-bold="true" forecolor="white" /> <editrowstyle backcolor="#999999" /> <alternatingrowstyle backcolor="white" forecolor="#284775" /> </asp:gridview> </div> </form> </body>
it looks aren't hooking databinding in bound fields example:
<asp:boundfield datafield="id" headertext="id"/>
you can find full example of how set databound field on msdn:here
Comments
Post a Comment