动态地将列值添加到DataGridview

时间:2022-12-15 13:21:09

I have three column in my datagridview .One is text ,one is Combo and another one is Text ...i don't want to use datasource want to add values on cell like datagridview.Rows[].cells[].value. Help me how i can do it? My database have several columns...How to add column value dynamically....

我的datagridview中有三列。一个是文本,一个是Combo,另一个是Text ...我不想使用datasource想要在datagridview.Rows [] .cell []。value等单元格上添加值。帮帮我怎么做?我的数据库有几列...如何动态添加列值....

3 个解决方案

#1


I just had to do the same exact type of thing...here is how you add a 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 the Gridview_rowcreated Sub

**在Gridview_rowcreated Sub中

e.row.cells(cellnumber from 0 to N).controls.Add(data)

you're going to have to create a connection and a connection string

你将不得不创建一个连接和一个连接字符串

here is 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 your datasource of the grid to the list

然后在页面加载时将网格的数据源设置为列表

hope this helps...if you have any questions just ask me.

希望这有帮助...如果您有任何问题,请问我。

#2


I would highly recommend to use a Repeater instead of datagridview and render as many columns as you want.

我强烈建议使用Repeater而不是datagridview并根据需要渲染任意数量的列。

.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 needs to be an array of the number of columns you want.

其中dayslist需要是您想要的列数的数组。

We have used the same approach to generate a complete Gantt Chart

我们使用相同的方法生成完整的甘特图

#3


Try something along the lines of

尝试一些类似的东西

dataGrid.Rows.Add(new object[] { "value1", 42, "value3"});

#1


I just had to do the same exact type of thing...here is how you add a 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 the Gridview_rowcreated Sub

**在Gridview_rowcreated Sub中

e.row.cells(cellnumber from 0 to N).controls.Add(data)

you're going to have to create a connection and a connection string

你将不得不创建一个连接和一个连接字符串

here is 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 your datasource of the grid to the list

然后在页面加载时将网格的数据源设置为列表

hope this helps...if you have any questions just ask me.

希望这有帮助...如果您有任何问题,请问我。

#2


I would highly recommend to use a Repeater instead of datagridview and render as many columns as you want.

我强烈建议使用Repeater而不是datagridview并根据需要渲染任意数量的列。

.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 needs to be an array of the number of columns you want.

其中dayslist需要是您想要的列数的数组。

We have used the same approach to generate a complete Gantt Chart

我们使用相同的方法生成完整的甘特图

#3


Try something along the lines of

尝试一些类似的东西

dataGrid.Rows.Add(new object[] { "value1", 42, "value3"});