有这样一个问题
                 我有一个Gridview要绑定数据,其中一列是一个下拉框,这个下拉框有两项(两项的值分别人1,0),而且这两项都是人为加上去的,也就是说下拉框的值是固定的,但Gridview绑定值的时候,下拉框对应的列取出来的是一个值,然后要求页面登录时下拉框选中的项与读出来的值要相对应(比如说,取出来的值为0,则下拉框选中的项的值为0,反之则行踪的项的值为1)    请问这个问题怎么解决?               各位高手帮一下忙,进者有分,回春越准确,分越高。   谢谢了各位!!!!

解决方案 »

  1.   

      /// <summary>
            /// 获取选中DataGridView的行
            /// </summary>
            /// <param name="dgv"></param>
            /// <returns></returns>
            public static System.Data.DataRow CurrentRow(DataGridView dgv)
            {
                System.Data.DataRowView view = null;
                CurrencyManager MyCurrencyManager;
                MyCurrencyManager = dgv.BindingContext[dgv.DataSource, dgv.DataMember] as CurrencyManager;
                if (MyCurrencyManager.Position >= 0)
                {
                    view = (System.Data.DataRowView)MyCurrencyManager.Current;
                }
                if (view != null)
                {
                    return view.Row;
                }
                else
                {
                    return null;
                }
            } System.Data.DataRow row = SystemManger.CurrentRow(dgvStuQK);//SystemManger是上面方法的类
              
                int id = int.Parse(row["Id"].ToString());//得到Gridview的值
      

  2.   

    在RowDataBound中事件中来选中呢?
      

  3.   


    <asp:GridView runat="server" ID="gvTest">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:DropDownList runat="server" ID="ddlTest">
                                    <asp:ListItem Value="0" Selected='<%# Eval("id").ToString().Equals("0") %>'>0</asp:ListItem>
                                    <asp:ListItem Value="1" Selected='<%# Eval("id").ToString().Equals("0") %>'>1</asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
      

  4.   

     int id = int.Parse(row["Id"].ToString());其中row["Id"]的Id是Gridview中的列名
      

  5.   

    1 .  查询数据源直接写上1,0,然后绑定 ,它能根据1,0自动select。
    2 .  RowDataBound事件中,再作一次处理 。
      

  6.   


    <asp:GridView runat="server" ID="gvTest">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:DropDownList runat="server" ID="ddlTest">
                                    <asp:ListItem Value="0" Selected='<%# Eval("id").ToString().Equals("0") %>'>0</asp:ListItem>
                                    <asp:ListItem Value="1" Selected='<%# Eval("id").ToString().Equals("1") %>'>1</asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
      

  7.   

    用RowDataBound事件判段,例如:
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[5].Text=="0")
                    {
                        e.Row.Cells[5].Text = "0";
                    }
                    else 
                    {
                        e.Row.Cells[5].Text = "1";
                    }
                   
                }
            }
      

  8.   


            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
                if (ddl != null)
                    ddl.SelectedValue = e.Row.Cells[0].Text;
            }
      

  9.   

    //看不懂,这跟 GridView 有什么关系啊. 你试试
    DropDownList ddl = new DropDownList();
    Session.Add("abc", ddl.SelectedValue);  //保存下拉框的值DropDownList ddlsigle = new DropDownList();//登陆页的下拉框
    ddlsigle.ClearSelection();
    ddlsigle.Items.FindByValue(Session["abc"].ToString()).Selected = true;
      

  10.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
            { 
                if (e.Row.RowType == DataControlRowType.DataRow) 
                { 
                    if (e.Row.Cells[5].Text=="0") 
                    { 
                        e.Row.Cells[5].Text = "0"; 
                    } 
                    else 
                    { 
                        e.Row.Cells[5].Text = "1"; 
                    } 
                  
                } 
            }
      

  11.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    int index = e.NewEditIndex;
    DropDownList drop = (DropDownList)ViewUsers.Rows[index].Cells["下拉框所在列索引"].FindControl("DropDownList1");
    drop.DataSource = 数据源(data());
    drop.DataTextField = "Name";
    drop.DataValueField = "Id";
    drop.DataBind();
    drop.SelectedIndex = data().Rows[index]["与下拉框对应的列"].ToString();
    }
      

  12.   

    补充一句 要加这个判断
     if (e.Row.RowType == DataControlRowType.DataRow) 
      

  13.   

    试试这个:
    this.ddl.SelectedValue=你取出来的值,看是0还是1了,要是字符串,如"1".
    这样就行了.
      

  14.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
                if (ddl != null)
                    ddl.SelectedValue = e.Row.Cells[0].Text;
            }
      

  15.   

    如果已经存到数据库中,应该从表中读出值,然后赋值给DropDownList !!