c# - Customizing a datagridview runtime -
i new c#,asp.net. creating small application want list data.
below mentioned function binds data 3 columns.
property name, property 'referenceid' , post date.
using (sqlconnection con = new sqlconnection(strconnstring)) { using (sqlcommand cmd = new sqlcommand("proc_recent_history_header")) { cmd.commandtype = commandtype.storedprocedure; cmd.parameters.addwithvalue("@id", member); cmd.connection = con; con.open(); reader = cmd.executereader(); gridview1.datasource = reader; gridview1.databind(); con.close(); }
asp.net
<asp:gridview id="gridview1" runat="server" class="form-control" width="100%" viewstatemode="enabled" autogeneratecolumns = "false"> <columns> <asp:boundfield datafield="prp_name" headertext="property name"/> <asp:boundfield datafield="prp_ref_no" headertext="reference"/> <asp:boundfield datafield="prp_crdt" headertext="post date"/> </columns> </asp:gridview>
i want display 2 columns along data. 1 'no.' serial number each rows , 1 column 'action', hyperlink, when click here page must redirected action based on 'property_referenceid' of corresponding row.
how add columns run time ?
here gridview1.datasource = reader; if consists of 3 columns bind 3 columns data.
step1: create columns manually adding columns step2: store data datatable
datatable datatable = new datatable(); datatable.load(cmd.executereader());
step3 : loop through each object datatable
datatable dt= new datatable(); datacolumn dc1 = new datacolumn("propertyname"); datacolumn dc2 = new datacolumn("propertyid"); datacolumn dc3 = new datacolumn("postdate"); datacolumn dc4 = new datacolumn("columnname4"); datacolumn dc5 = new datacolumn("columnname5"); dt.columns.add(dc1); dt.columns.add(dc2); dt.columns.add(dc3); dt.columns.add(dc4); dt.columns.add(dc5); foreach (datarow row in datatable.rows) { datarow dr = dt.newrow(); dr[0] = row[propertyname]; dr[1] = row[propertytype]; dr[2] = row[postdate]; dr[3] = "yes"; dr[4] = "no"; dt.rows.add(dr); } gridview1.datasource = dt;
Comments
Post a Comment