Dynamically adding column value to DataGridview
I have three columns in my datagridview. One is Text, one is Combo and the other is Text ... I don't want to use datasource to add values ββto a cell like datagridview.Rows []. cells []. cost. Help me how can i do this? My database has multiple columns ... How do I add a column value dynamically ....
a source to share
I just needed to do the exact exact type ... this is how you add the column.
If Not IsPostBack Then <br>
Dim field As New TemplateField
field.HeaderText = "Name of Column"
Dim col As DataControlField = field
GridView.Columns.Add(col)
End If
** In Gridview_rowcreated Sub
e.row.cells(cellnumber from 0 to N).controls.Add(data)
you will need to create a connection and a connection string
here's an example ...
Dim Dbconn As SqlConnection
Dim Dbcmd As SqlCommand
Dbcmd = New Data.SqlClient.SqlCommand()
Dbcmd.Connection = Dbconn
Dbcmd.CommandType = Data.CommandType.Text
Dbcmd.Commandtext = "select * from table"
dbconn.open()
//then you need a data reader
dim dr as sqlclient.sqldatareader
dr = dbcmd.executereader
while dr.read
add each item to a list
end while
then on page load, set grid datasource to list
hope this helps ... if you have any questions just ask me.
a source to share
I highly recommend using Repeater instead of datagridview and displaying as many columns as you like.
.aspx code
<tr>
<asp:Repeater ID="rptDayHeaders" runat="server">
<ItemTemplate>
<td>
<strong><asp:Literal ID="ltMonthHeader" runat="server"></asp:Literal></strong>
</td>
</ItemTemplate>
</asp:Repeater>
</tr>
.aspx.vb code
rptDayHeaders.DataSource = daysList rptDayHeaders.DataBind()
where dayslist should be an array of the number of columns you want.
We used the same approach to create a complete Gantt chart
a source to share