单向数据绑定gridview - 仅更新?

时间:2012-05-29 18:10:16

标签: c# asp.net data-binding gridview

我正在使用gridview做一些复杂的事情,我和完成之间唯一的事情是我不能以编程方式设置数据绑定。除了我仍然无法获取要更新的数据源之外,我已经完成了大部分问题。

我用一个依赖的下拉列表替换了一个输入字段,并得到它以保留数据绑定字段的值。我不能直接对它进行数据绑定,因为我得到“选择值不在项目列表中”或者其他东西,所以我必须找到一种方法来获取网格或数据源从该依赖下拉中获取值并将其应用于表

任何帮助?

<asp:GridView ID="gvManager" runat="server" 
        AutoGenerateColumns="False" DataSourceID="ldsCampaigns" 
        ondatabound="gvManager_DataBound" 
        onrowediting="gvManager_RowEditing" DataKeyNames="ContentID">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="ContentID" HeaderText="ContentID" 
                SortExpression="ContentID" />
            <asp:BoundField DataField="CampaignName" HeaderText="CampaignName" 
                SortExpression="CampaignName" />
            <asp:BoundField DataField="CampaignTitle" HeaderText="CampaignTitle" 
                SortExpression="CampaignTitle" />
            <asp:BoundField DataField="CampaignTagLine" HeaderText="CampaignTagLine" 
                SortExpression="CampaignTagLine" />
            <asp:TemplateField HeaderText="CampaignData" SortExpression="CampaignData">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ldsTables" 
                        DataTextField="CMSTables" DataValueField="CMSTables" 
                        SelectedValue='<%# Bind("CampaignData") %>' onload="DropDownList1_Load">
                    </asp:DropDownList>
                    <asp:LinqDataSource ID="ldsTables" runat="server" 
                        ContextTypeName="DataContext" 
                        onselecting="ldsTables_Selecting" Select="new (CMSTables)" 
                        TableName="CampaignTables">
                    </asp:LinqDataSource>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("CampaignData") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="DataColumn" 
                SortExpression="DataColumn">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server" style="margin-bottom: 0px" 
                        SelectedValue='<%# Bind("DataColumn") %>' Visible="False">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" 
                        Text='<%# Bind("DataColumn") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="CampaignLink" HeaderText="CampaignLink" 
                SortExpression="CampaignLink" />
            <asp:BoundField DataField="Sunrise" HeaderText="Sunrise" 
                SortExpression="Sunrise" />
            <asp:BoundField DataField="Sunset" HeaderText="Sunset" 
                SortExpression="Sunset" />
            <asp:BoundField DataField="OwnerID" HeaderText="OwnerID" 
                SortExpression="OwnerID" />
            <asp:CheckBoxField DataField="Enabled" HeaderText="Enabled" 
                SortExpression="Enabled" />
        </Columns>
    </asp:GridView>

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题是正确的,你有一个gridview,在gridview内有一个下拉列表,在更改你想要获取它的值的下拉列表并获取一些数据或任何你想要的东西,如果是这样的话你拥有的是以下内容:

1 - 设置你的下拉列表属性AutoPostBack =“true”。

2 - 在下拉列表的dropdownlist_indexchanged事件中执行以下操作:

protected void dropdownlist1_indexchanged(sender object,eventargs e)
{
   //Because the dropdownlist is inside the gridview you can't get data directly
   //You will have to parse the sender object which will retrieve the dropdownlist
   DropdownList ddl = (DropdownList)sender;
   //You can then extract the data from the dropdownlist
}

答案 1 :(得分:0)

对于任何未来的绝望程序员,我必须更新更新事件的linq数据源代码:

    protected void ldsCampaigns_Updated(object sender, LinqDataSourceStatusEventArgs e)
    {
        using (DashboardDataContext c = new DashboardDataContext())
        {
            Label Label3 = gvManager.Rows[gvManager.EditIndex].FindControl("Label4") as Label;

            List<CMSCampaigns> change = c.CMSCampaigns.Where(s => s.ContentID == Convert.ToInt16(Label3.Text)).ToList();

            DropDownList DropDownList2 = gvDashboardManager.Rows[gvDashboardManager.EditIndex].FindControl("DropDownList2") as DropDownList;


            change[0].DataColumn = DropDownList2.SelectedValue.ToString();

            c.SubmitChanges();
        }
    }

这是我曾经克服过的最集群的集群库。