代码很简单,实现的功能是,如果点击Edit (自带的),出现的Dropdownlist的初始选择值等于点击Edit以前的值public partial class Test2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Label mytext = (Label)(GridView1.Rows[e.NewEditIndex].FindControl("Label1"));
   
        this.Label2.Text = mytext.Text;        DropDownList mylist = (DropDownList)(GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1"));        mylist.ClearSelection(); (运行的时候报错为:Object reference not set to an instance of an object.)        mylist.Items.FindByValue(mytext.Text).Selected = true;    }
}请问应该怎么修改啊!!!!!!急,想了几天了

解决方案 »

  1.   

    查了一下msdn:http://msdn.microsoft.com/zh-cn/library/bxdf3ekh(VS.80).aspx每一列和每一行的 Selected 属性都设置为 false。这时候你那个dropdownlist的选择的值是null!会报错!
      

  2.   

    点编辑的时候值没有初始化 选中的是null 可以先将取也来的值添加dropDownlist中并设置默认选择项
      

  3.   

    我是这样绑定的
    <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" SelectedIndex='<%# GetSelectedTitle(Eval("Cat")) %>' (绑定)
    DataSource='<%# Cat %>' >
                                <asp:ListItem>Mr.</asp:ListItem>
                                <asp:ListItem>Mrs.</asp:ListItem>
                                <asp:ListItem>Dr.</asp:ListItem>
                                <asp:ListItem>Ms.</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
     protected string[] Cat
        {
            get { return new string[] { "Mr.", "Dr.", "Ms.", "Mrs." }; }
        }    protected int GetSelectedTitle(object title)
        {
            return Array.IndexOf(Cat, title.ToString());
        }
      

  4.   

      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
                DropDownList mylist= ((DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1")).SelectedValue.Trim();
    if(mylist!=null)
    {}
    }
    在protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowIndex != -1)
                {
              if ((DropDownList)e.Row.FindControl("DropDownList1") != null)
                    {
                   ((DropDownList)e.Row.FindControl("DropDownList1")).SelectedValue = "";                }
                 }
             }
      

  5.   

     楼上:DropDownList mylist= ((DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1")).SelectedValue.Trim(); 报错:错误 2 无法将类型“string”隐式转换为“System.Web.UI.WebControls.DropDownList” G:\My Project\CDPCS\Test2.aspx.cs 28 31 G:\My Project\CDPCS\
      

  6.   

    目前代码修改成这样了
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;        DropDownList mylist = ((DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1")).SelectedValue.Trim();        if (mylist != null) (这里虽然不报错,但是因为mylist==null,所以会跳过这里的内容,无法得到想要的结果)
            {
                Label mytext = (Label)(GridView1.Rows[e.NewEditIndex].FindControl("Label1"));            this.Label2.Text = mytext.Text;            mylist.ClearSelection();            mylist.Items.FindByValue(mytext.Text).Selected = true;
            } 
              }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                if ((DropDownList)e.Row.FindControl("DropDownList1") != null)
                {
                    ((DropDownList)e.Row.FindControl("DropDownList1")).SelectedValue = "";
                }
            }
        }
      

  7.   

    rowediting时似乎无法取得edititemtemplate里的控件,你把这个模板列改成只有itemtemplate的形式,把dropdownlist放到ItemTemplate里
      

  8.   

    DropDownList mylist= ((DropDownList)GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1"));
      

  9.   

    空引用了,看看哪个dropdownlist有值没
      

  10.   


    如果放入ItemTemplate,那么就没法在Edit的时候实现我想要的功能了
      

  11.   

    貌似DataList是可以实现我想要的功能的,是吗?
      

  12.   

    哥们你事件写错了.我也碰见过,呵呵,参考下我的 代码吧,呵呵 //行绑定的时候从数据库表中取出对应的名称放在dropdownlist下拉列表中
            protected void GvQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowIndex == GvQuestion.EditIndex)
                {
                    DropDownList ddl = e.Row.FindControl("ddl_name") as DropDownList;
                    if (ddl != null)
                    {
                        String name = (e.Row.DataItem as DataRowView).Row.ItemArray[1].ToString();
                        for (int i = 0; i < ddl.Items.Count; i++)
                        {
                            if (ddl.Items[i].Text.Trim() == name.Trim())
                            {
                                ddl.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }
      

  13.   

    //行绑定的时候从数据库表中取出对应的名称放在dropdownlist下拉列表中
            protected void GvQuestion_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowIndex == GvQuestion.EditIndex)
                {
                    DropDownList ddl = e.Row.FindControl("ddl_name") as DropDownList;
                    if (ddl != null)
                    {
                        String name = (e.Row.DataItem as DataRowView).Row.ItemArray[1].ToString();
                        for (int i = 0; i < ddl.Items.Count; i++)
                        {
                            if (ddl.Items[i].Text.Trim() == name.Trim())
                            {
                                ddl.SelectedIndex = i;
                                break;
                            }
                        }
                    }
                }
            }ding首先判断是否为空!
      

  14.   

    1 using System;   
     2 using System.Data;   
     3 using System.Data.SqlClient;   
     4 using System.Configuration;   
     5 using System.Collections;   
     6 using System.Web;   
     7 using System.Web.Security;   
     8 using System.Web.UI;   
     9 using System.Web.UI.WebControls;   
    10 using System.Web.UI.WebControls.WebParts;   
    11 using System.Web.UI.HtmlControls;   
    12    
    13 public partial class Demo13 : System.Web.UI.Page   
    14 {   
    15     protected void Page_Load(object sender, EventArgs e)   
    16     {   
    17         if (Page.IsPostBack == false)   
    18         {   
    19             BindData();   
    20         }   
    21     }   
    22    
    23     public void BindData()   
    24     {   
    25         string strSql = "select UserID,C_Name,E_Name,QQ,Blood_Type from Demo_User ";   
    26         DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.CONN_STRING, CommandType.Text, strSql, null).Tables[0];   
    27    
    28         GridView.DataSource = dt;   
    29         GridView.DataKeyNames = new string[] { "UserID" };//主键   
    30         GridView.DataBind();   
    31     }   
    32    
    33     protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)   
    34     {   
    35         GridView.PageIndex = e.NewPageIndex;   
    36         BindData();   
    37     }   
    38    
    39     protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)   
    40     {   
    41         if (((DropDownList)e.Row.FindControl("DropDownList")) != null)   
    42         {   
    43             DropDownList DropDownList = (DropDownList)e.Row.FindControl("DropDownList");   
    44             DropDownList.SelectedValue = ((HiddenField)e.Row.FindControl("HiddenField")).Value;   
    45         }   
    46     }   
    47    
    48     protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)   
    49     {   
    50         DropDownList DropDownList = sender as DropDownList;   
    51         int index = (DropDownList.NamingContainer as GridViewRow).RowIndex;   
    52         string UserID = GridView.DataKeys[index].Value.ToString();   
    53    
    54         string strSql = "Update Demo_User set Blood_Type=@Blood_Type where UserID=@UserID ";   
    55         SqlParameter[] para = {   
    56                                 new SqlParameter("@Blood_Type", DropDownList.SelectedValue.ToString()),   
    57                                 new SqlParameter("@UserID", UserID),   
    58                                };   
    59         SqlHelper.ExecuteNonQuery(SqlHelper.CONN_STRING, CommandType.Text, strSql, para);   
    60         BindData();   
    61     }   
    62 }    
      
     1 <table align="center" bgcolor="#c0de98" border="0" cellpadding="0" cellspacing="1" width="99%" >    
     2         <tr>   
     3             <th colspan="2">   
     4                 GridView演示</th>   
     5         </tr>        
     6        <tr>   
     7            <td colspan="2" style="width: 100%;" >   
     8               <asp:GridView ID="GridView" runat="server" Width="100%" AutoGenerateColumns="False" AllowPaging="True" OnPageIndexChanging="GridView_PageIndexChanging" PageSize="12" OnRowDataBound="GridView_RowDataBound" >   
     9                 <Columns>   
    10                       <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True" />   
    11                       <asp:BoundField DataField="C_Name" HeaderText="中文名字" ReadOnly="True" />   
    12                       <asp:BoundField DataField="E_Name" HeaderText="英文名字" ReadOnly="True" />   
    13                       <asp:BoundField DataField="QQ" HeaderText="QQ" />   
    14                     <asp:TemplateField HeaderText="学生血型">   
    15                         <ItemTemplate>   
    16                             <asp:DropDownList ID="DropDownList" runat="server" OnSelectedIndexChanged="DropDownList_SelectedIndexChanged" AutoPostBack="True">   
    17                               <asp:ListItem Value="1">A</asp:ListItem>   
    18                               <asp:ListItem Value="2">B</asp:ListItem>   
    19                               <asp:ListItem Value="3">AB</asp:ListItem>   
    20                               <asp:ListItem Value="4">O</asp:ListItem>        
    21                             </asp:DropDownList>   
    22                             <asp:HiddenField ID="HiddenField" runat="server" Value='<%# Eval("Blood_Type") %>'/>   
    23                         </ItemTemplate>   
    24                     </asp:TemplateField>   
    25                   </Columns>   
    26                   <RowStyle HorizontalAlign="Center" />     
    27                   <PagerStyle HorizontalAlign="Right" />   
    28               </asp:GridView>   
    29            </td>   
    30        </tr>          
    31 </table>   
       
      

  15.   


    在模板列中放入两个控件,一个是DropDownList,另一个是HiddenField,让HiddenField绑定选择的值,然后在事件DataBound时用,先判断GridView1.CurrentMode==DetailsViewMode.Edit时,查找出DropDownList,让它的值等于你隐藏控件HiddenField中的值就可以了。