在数据库的Gridview下拉列表中设置选定的值 [英] Setting Selected Value in a Drop Down of Gridview from Database

查看:72
本文介绍了在数据库的Gridview下拉列表中设置选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网格视图中进行编辑时,我想要下拉列表中特定字段的旧值。



目前当我在gridview中进入编辑模式时我正在列表项中指定的下拉列表。



我直接尝试了Selected Value属性,但它在网格视图中不起作用。实际上我也没有在Intellisense中找到它。



我找到了一个解决方案,但它只用于奇数行,甚至行也没有显示相同的东西。



例如,如果我在进入编辑模式时将某人指定为Web Designer,则下拉列表会显示数据成员中的第一个销售额。



添加一些代码后,我认为我解决了问题,但经过仔细测试,我发现偶数行显示数据成员的第一个值,即Sales。但是对于Odd行它的工作正常。



代码:



During editing in a grid view I want the old values of the particular field in the drop down.

Currently when I enter the edit mode in the gridview i am getting the dropdown as i specified in the list item.

I tried the Selected Value attribute directly but its not working in the grid view. Actually i didn''t find it in the Intellisense also.

I found a solution but its working for only odd rows, even rows are not getting showing the same thing.

e.g If i set someones designation as Web Designer when entering the edit mode the dropdown shows Sales which is first in the Data Members.

After adding some code I thought i solved the problem but on closer testing i found the even rows are showing the first value of data member i.e Sales. But for Odd rows its working properly.

The Code:

protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton l = (LinkButton)e.Row.FindControl("lnkgrdDelete");
            l.Attributes.Add("onclick", "javascript:return confirm('Are You Sure');");

        }
        if (e.Row.RowState == DataControlRowState.Edit)
        {
            DropDownList ddl1 = (DropDownList)e.Row.FindControl("ddlgrdDesg");
            DropDownList ddl2 = (DropDownList)e.Row.FindControl("ddlgrdUType");

            UserBAL user = new UserBAL();

            int key = Convert.ToInt32(grdUsers.DataKeys[e.Row.RowIndex].Value.ToString());
            string[] data = user.GetUserByID(key);

            ddl1.SelectedItem.Text = data[4];
            ddl2.SelectedItem.Text = data[5];
        }
    }





此代码适用于奇数行但不适用于偶数行。我不明白一些非常有趣的事情。从我的观点来看,这是正确的。同样在调试期间,偶数行永远不会进入if条件,因为e.Row.RowState是正常的。



This code works for odd rows but not for even rows. I dont understand something very funny. Its right from my point of view. Also during debugging the even rows never enter the if condition as e.Row.RowState is normal then.

推荐答案

public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
       {
string TempValue="";
TempValue =dataGridView1.Rows[e.RowIndex].Cells["ColumnName"].Value.ToString();
UrDrowDownThing.datasource=TempValue;
}


这篇关于在数据库的Gridview下拉列表中设置选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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