我如何绑定数据使用VB数据库表中的下拉列表中的一个gridview? [英] How do I data bind a drop down list in a gridview from a database table using VB?

查看:309
本文介绍了我如何绑定数据使用VB数据库表中的下拉列表中的一个gridview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,在这个GridView中,有一个状态栏,我想有一个下拉列表,给出合格,尚待解决,点击编辑按钮时出现失败。这些数值都已经在一个表中,所以我需要以某种方式从该表绑定到每个DDL的每一行。

So in this gridview, there is a column for status and I want to have a drop down list with Pass, Pending, Fail appear when the edit button is clicked. These values are already in a table, so I need to somehow bind from this table to each ddl for every row.

下面是在GridView的列。正如你所看到的,我想只是显示了当没有在编辑模式下一个标签,和一个DDL时编辑按钮pssed $ P $

Here is the column from the gridview. As you can see, I would like to just have a label showing when not in edit mode, and a ddl when the edit button is pressed

    <asp:TemplateField HeaderText="During Production Status" SortExpression="DuringProductionStatus">
        <EditItemTemplate>
             <asp:DropDownList ID="ddlStatus" runat="server" datavaluefield="Name" 
                 datatextfield="Name" DataSource="*What goes here?*"> />
        </EditItemTemplate>
        <ItemTemplate>
            <asp:Label ID="lblStatus" runat="server"
                Text='I don't understand how to get this from the ddl' />
        </ItemTemplate>
    </asp:TemplateField>

为了清楚起见,我的表名为状态,数据库被命名为DirectImport

For clarity's sake, my table is named Status, and the database is named DirectImport

推荐答案

有几个步骤都要经过这里 - 他们都不是特别困难,但他们可能会有点的繁琐的(恕我直言) 。好消息是,一旦你得到了这个工作一次,它变得更容易做一遍!

There's a few steps to go through here - none of them are particularly difficult, but they can be a bit fiddly (IMHO). The good news is once you've got this working once, it gets easier to do it again!

我假设你有一个&LT; ASP:*数据源&GT; 页面上控制 - 我的preference是一个ObjectDataSource,但我不认为根本问题上,我觉得一个SqlDataSource同样适用。我从来没有尝试在$ C与 GridView.DataSource = MyDataSet 操作$ C-后面,所以我不知道是否会工作或没有,但我的假设是,它不会因为你不会得到适当的双向绑定,你想要的。此数据源饲料网格与您的主要数据。这里的关键是,你的数据库查询必须返回两个状态文本字段和状态ID。

I'm assuming you've got a <asp:*DataSource> control on your page - my preference is for an ObjectDataSource but I don't think it matters, I think a SqlDataSource works equally well. I've never tried doing this with GridView.DataSource = MyDataSet in code-behind, so I don't know whether that would work or not, but my assumption is that it wouldn't as you wouldn't get the proper two-way binding that you want. This data source feeds your grid with your main data. The key point here is that your database query must return both the Status text field and the Status id.

所以你的GridView现在看起来是这样的:

So your gridview will now look something like:

<asp:objectdatasource runat="server" id="MainDataSource" ... />  
<asp:gridview runat="server" id="MyGridView" DataSourceID="MainDataSource">  
    <Columns>  
        <asp:TemplateField HeaderText="During Production Status" SortExpression="DuringProductionStatus">  
            <ItemTemplate>  
                <asp:Label ID="lblStatus" runat="server"  
                   Text="<%# Bind('Status') %>" />  
           </ItemTemplate>  
        </asp:TemplateField>  
     </Columns>  
</asp:gridview>

文本=&LT;%#绑定(状态)%&gt;中是你错过了拿到状态文成电网的位。

The Text="<%# Bind('Status') %>" is the bit you're missing to get the status text into the grid.

现在添加第二个数据源到您的标记,在从状态表中的设定值的读取。

Now add a second data source into your markup that reads in the set of values from the Status table.

<asp:objectdatasource runat="server" id="StatusObjectDataSource" ... />

和添加EditItemTemplate中到GridView控件,这势必状态的数据源。

And add the EditItemTemplate into the GridView, which is bound to the Status DataSource.

<EditItemTemplate>
    <asp:DropDownList ID="ddlStatus" runat="server" datavaluefield="StatusID" 
        datatextfield="Status" DataSourceID="StatusObjectDataSource"  
        SelectedValue="<%# Bind('StatusId') %>" />
</EditItemTemplate>

的SelectedValue =&LT;%#绑定('StatusId')%&gt;中是两个数据集,这样,当你翻转一行到编辑什么连接起来模式,将DropDownList已经选择了正确的项目,当你保存它,你已经得到了状态ID投入到你的数据库。结果
就大功告成了。

The SelectedValue="<%# Bind('StatusId') %>" is what connects up the two datasets so that when you flip a row into Edit mode, the dropdownlist has the correct item already selected, and when you then save it you've got the Status ID to put into your database.
And you're done.

这篇关于我如何绑定数据使用VB数据库表中的下拉列表中的一个gridview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆