C1GridView: 添加 DropdownList

发布时间:2013/01/07 00:01 发布者:iceman

返回博客中心

我们可以添加多种单元格类型到 C1GridView。链接中的文档详细的阐述了C1GridView内置的单元格类型。在这些单元格类型中,C1TemplateField 可以通过定制来满足不同的用户需求。我们可以通过 C1TemplateField  下ItemTemplate 属性,在 Grid 加载时绑定数据源到 C1TemplateField。同时,我们可以通过 EditItemTemplate 属性作为 C1TemplateField 的编辑器。可以参考 “添加控件到 C1GridView 列”详细了解添加控件到 C1Templatefield 的方法。

在本篇文章中我们将重点讲述如何在 C1GridView 中添加 DropDownList控件到模板列中。当我们给 C1GridView 绑定 Customer 表时,Country 字段也被添加到表格的列中。

下面代码展示如何在设计时绑定模板列:

 

<wijmo:C1GridView ID="C1GridView1" runat="server" AutogenerateColumns="False" DataKeyNames="CustomerID"
        DataSourceID="AccessDataSource1" OnRowUpdating="C1GridView1_RowUpdating">
        <Columns>
            <wijmo:C1BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
                SortExpression="CustomerID">
            </wijmo:C1BoundField>
            <wijmo:C1BoundField DataField="CustomerName" HeaderText="CustomerName" SortExpression="CustomerName">
            </wijmo:C1BoundField>
            <wijmo:C1TemplateField HeaderText="Country">
                <ItemTemplate>
                    <span>
                        <%# Eval("Country") %></span>
                </ItemTemplate>
                <EditItemTemplate>
                      <asp:DropDownList ID="dlCountry" runat="server" DataSourceID="AccessDataSource2"
                        DataTextField="Country" DataValueField="Country" SelectedValue='<%# Bind("Country") %>'>
                    </asp:DropDownList>
                    <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="~/App_Data/C1NWind1.mdb"
                        SelectCommand="SELECT * FROM [Countries]"></asp:AccessDataSource>
                </EditItemTemplate>
            </wijmo:C1TemplateField>
            <wijmo:C1CommandField ShowEditButton="True">
            </wijmo:C1CommandField>
        </Columns>
    </wijmo:C1GridView>
    <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/C1NWind1.mdb"
        SelectCommand="SELECT * FROM [Customers]" >
    </asp:AccessDataSource>

下列图片展示了允许程序的初始界面:

 

下面,我们可以点击 Edit 按钮使 C1GridView 更新到编辑状态。我们可以看到 DropDownList 弹出,通过 DropDownList 我们可以选择目标项。截图:

 

在选择目标选项后,点击 Update 按钮完成更新,此时,我们需要通过 UpdateCommand 事件来更新数据源,实现代码如下:

protected void C1GridView1_RowUpdating(object sender, C1.Web.Wijmo.Controls.C1GridView.C1GridViewUpdateEventArgs e)
    {
      AccessDataSource1.UpdateCommand = "Update Customers Set CustomerName=@CustomerName, Country=@Country where CustomerID=@CustomerID";
      AccessDataSource1.UpdateParameters.Add("CustomerID",e.Keys["CustomerID"].ToString());
      AccessDataSource1.UpdateParameters.Add("CustomerName", e.NewValues["CustomerName"].ToString());
      AccessDataSource1.UpdateParameters.Add("Country", e.NewValues["Country"].ToString());
      AccessDataSource1.Update();
    }
 

更详细信息请参考 Demo:

Download Sample


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网