更新的GridView与DropDownList控件 [英] Update GridView with Dropdownlist control

查看:185
本文介绍了更新的GridView与DropDownList控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用VS2005 C#。

目前我有一个GridView,我已经改变了我的GridView控件之一,我的专栏名称性别,从默认的文本框的DropDownList ,这是我给了 ID 控件的 GenderList ,它包含2个值, M 的和的˚F

我有一个默认的更新语句,它能够后更新GridView的修改,这是以下内容:

 < ASP:SqlDataSource的ID =SqlDataSource1=服务器的ConnectionString =
&所述;%$的ConnectionStrings:SODConnectionString%>中更新命令=UPDATE
[UserMasterData] SET [名称] = @Name,[年龄] = @Age,[ContactNo] = @ ContactNo,
 [联系地址] = @地址,[两性] = @性别/>

以上UPDATE查询作品完美,现在我已经改变了我的性别文本的到的的DropDownList 的中, 更新查询给了我该说的错误:

必须声明标量变量@Gender。

我假定UPDATE查询无法找到我的性别值列。

我试图修改更新查询 @GenderList ,但它并没有正常工作。

任何人都知道我应该做的做更新查询,使我的更新查询可以找到我的 GenderList 的DropDownList在我的性别值列?

感谢您。


下面是我的previous 性别柱TextBox控件:

 < ASP:BoundField的的HeaderText =性别
                数据字段=性别
                SORTEX pression =性别>< / ASP:BoundField的>

下面是我的性别与DropDownList控件:

 < ASP:的TemplateField的HeaderText =性别SORTEX pression =性别>
        <&EditItemTemplate的GT;
            < ASP:DropDownList的ID =GenderList=服务器WIDTH =50像素>
                < ASP:ListItem的> M< / ASP:ListItem的>
                < ASP:ListItem的> F< / ASP:ListItem的>
            < / ASP:DropDownList的>
        < / EditItemTemplate中>
        <&ItemTemplate中GT;


编辑:

试过实施 RowDatBound onRowUpdating


RowDatBound


 保护无效GridView1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    DataRowView的dRowView =(DataRowView的)e.Row.DataItem;
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        如果((e.Row.RowState&放大器; DataControlRowState.Edit)大于0)
        {
            DropDownList的genderList =(DropDownList的)e.Row.FindControl(GenderList);
            genderList.SelectedValue = dRowView [2]的ToString();
        }
    }
}


RowUpdating .aspx.cs


 保护无效GridView1_RowUpdating(对象发件人,GridViewUpdateEventArgs E)
{DropDownList的genderSelect =(DropDownList的)GridView1.Rows [e.RowIndex] .FindControl(GenderList);SqlDataSource1.UpdateParameters [性别。默认值=
genderSelect.SelectedValue; - >错误说未设置为一个对象的一个​​实例}


解决方案

如果您使用的是SqlDataSource和更新,然后通过它的数据的所有您需要做的是设置2路为DropDownList的GenderList结合。

您可以通过设计器中设置这样或直接在源还

 <&EditItemTemplate的GT;
                < ASP:DropDownList的ID =GenderList=服务器
                    的SelectedValue ='<%#绑定(性别)%GT;'>
                    < ASP:ListItem的> M< / ASP:ListItem的>
                    < ASP:ListItem的> F< / ASP:ListItem的>
                < / ASP:DropDownList的>
            < / EditItemTemplate中>

通知该绑定正在使用此2路。

I am using VS2005 C#.

Currently I have a GridView, and I have changed one of my GridView control to my column name Gender, from the default TextBox to a DropDownList, which I gave the ID of the control to GenderList, and it contains 2 values, M and F.

I have a default update statement which is able to update the GridView after edit, which is the following:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=
"<%$ ConnectionStrings:SODConnectionString %>" UpdateCommand="UPDATE 
[UserMasterData] SET [Name] = @Name, [Age] = @Age, [ContactNo]=@ContactNo,
 [Address]=@Address, [Gender]=@Gender"/>

The above UPDATE query works perfectly, and now I have changed my Gender textbox to a dropdownlist, the UPDATE query gave me an error which says:

Must declare the scalar variable "@Gender".

I assume the UPDATE query couldn't find the value from my Gender column.

I tried to modify the UPDATE query to @GenderList, but it did not work as well.

Anyone knows what I should do do the UPDATE query so that my UPDATE query can find the value from my GenderList dropdownlist in my Gender column?

Thank you.


Below is my previous Gender column with a textbox control:

                <asp:BoundField  HeaderText="Gender" 
                DataField="Gender" 
                SortExpression="Gender"></asp:BoundField>

Below is my Gender with the dropdownlist control:

    <asp:TemplateField HeaderText="Gender" SortExpression="Gender" >
        <EditItemTemplate>
            <asp:DropDownList ID="GenderList" runat="server" Width="50px" >
                <asp:ListItem>M</asp:ListItem>
                <asp:ListItem>F</asp:ListItem>
            </asp:DropDownList>
        </EditItemTemplate>
        <ItemTemplate>


EDIT:

Tried implementing RowDatBound and onRowUpdating:


RowDatBound


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    DataRowView dRowView = (DataRowView)e.Row.DataItem;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
            DropDownList genderList= (DropDownList)e.Row.FindControl("GenderList");
            genderList.SelectedValue = dRowView[2].ToString();
        }
    }
}


RowUpdating.aspx.cs


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

DropDownList genderSelect =(DropDownList)GridView1.Rows[e.RowIndex].FindControl("GenderList");

SqlDataSource1.UpdateParameters["Gender"].DefaultValue = 
genderSelect.SelectedValue; --> error says not set to an instance of an object

}

解决方案

If you are using SqlDataSource and updating data via it then all you have to do is to set 2 way binding for the dropdownlist GenderList.

You can set this via the designer or directly in source also

 <EditItemTemplate>
                <asp:DropDownList ID="GenderList" runat="server" 
                    SelectedValue='<%# Bind("Gender") %>'>
                    <asp:ListItem>M</asp:ListItem>
                    <asp:ListItem>F</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>

notice that here 2 way binding is being used.

这篇关于更新的GridView与DropDownList控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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