0

Dropdownlist 动态绑定到gridview。它总是在gridview 的第一行。当我尝试获取下拉列表值时,它给出空值:

Here is code to bind dropdownlist to gridview:

protected void dgData_RowDataBound(object sender, GridViewRowEventArgs e)
{

    ArrayList strHead = null;
    if (e.Row.RowType == DataControlRowType.Header)
    {
        strHead = new ArrayList();
        foreach (DataControlFieldCell cell in e.Row.Cells)
        {
            strHead.Add(cell.Text);
        }
        ViewState["HeaderCell"] = strHead;
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddl;


        if (e.Row.RowIndex == 0)
        {
            for (Int32 i = 0; i < e.Row.Cells.Count; i++)
            {
                ddl = new DropDownList();
                ddl.ID = "ddlCol" + i.ToString();
                e.Row.Cells[i].Controls.Add(ddl);
                ddl.Items.Insert(0, new ListItem(string.Empty, string.Empty));
                ddl.Items.Insert(1, new ListItem("First Name", "1"));
                ddl.Items.Insert(2, new ListItem("Last Name", "2"));
                ddl.Items.Insert(3, new ListItem("Company Name", "3"));
                ddl.Items.Insert(4, new ListItem("Account No", "4"));
                ddl.Items.Insert(5, new ListItem("Account No 2", "5"));
                ddl.Items.Insert(6, new ListItem("Account No 3", "6"));
                ddl.Items.Insert(7, new ListItem("Groups", "7"));
                ddl.Items.Insert(8, new ListItem("Business Phone", "8"));
                ddl.Items.Insert(9, new ListItem("Home Phone", "9"));
                ddl.Items.Insert(10, new ListItem("Mobile", "10"));
                ddl.Items.Insert(11, new ListItem("Email", "11"));
                ddl.Items.Insert(12, new ListItem("Other Phone Number", "12"));
                ddl.Items.Insert(13, new ListItem("Web Page", "13"));
                ddl.Items.Insert(14, new ListItem("IM Address", "14"));
                ddl.Items.Insert(15, new ListItem("Comment", "15"));
                ddl.Items.Insert(16, new ListItem("Street Address 1", "16"));
                ddl.Items.Insert(17, new ListItem("Street Address 2", "17"));
                ddl.Items.Insert(18, new ListItem("Street City / Suburb", "18"));
                ddl.Items.Insert(19, new ListItem("Street State", "19"));
                ddl.Items.Insert(20, new ListItem("Street Post Code", "20"));
                ddl.Items.Insert(21, new ListItem("Street Country", "21"));
                ddl.Items.Insert(22, new ListItem("Mailing Address 1", "22"));
                ddl.Items.Insert(23, new ListItem("Mailing Address 2", "23"));
                ddl.Items.Insert(24, new ListItem("Mailing City / Suburb", "24"));
                ddl.Items.Insert(25, new ListItem("Mailing State", "25"));
                ddl.Items.Insert(26, new ListItem("Mailing Post Code", "26"));
                ddl.Items.Insert(27, new ListItem("Mailing Country", "27"));
                ddl.Items.Insert(28, new ListItem("Notes", "28"));
                ddl.Items.Insert(29, new ListItem("Custom Fields", "29"));
                ddl.Items.Insert(30, new ListItem("Relationship Manager", "30"));
                ddl.Items.Insert(31, new ListItem("Primary Interest", "31"));
                ddl.Items.Insert(32, new ListItem("Account Manager", "32"));
                strHead = (ArrayList)ViewState["HeaderCell"];




                foreach(ListItem item in ddl.Items)
        {
             if (strHead[i].ToString() == item.Text)
             {
            ddl.SelectedValue = item.Value;
             }
            else
             {
         // ddl.SelectedIndex = 0;
            }


                }

            }



         }

    }

  }

绑定gridview的代码:

 private void grdbind()
{


    DataTable db = new DataTable();
    db = (DataTable)Session["csvdata"];
    DataRow newrow = db.NewRow();
    db.Rows.InsertAt(newrow, 0);

    colcount = db.Columns.Count;

    dgData.DataSource = db;
    dgData.DataBind();


  }

这是在我遇到问题的按钮单击时获取下拉列表值的代码:

   protected void Button1_Click(object sender, EventArgs e)
{
     GridViewRow row;
    row = dgData.Rows[0];
   DropDownList ddl= (DropDownList)(row.Cells[1].FindControl("ddlCol1"));

}
4

1 回答 1

0

如果您有标题行,它将始终位于索引 0 处,因此您无法测试它是否不是标题行且索引为 0,因为这两项检查都将返回 false。

在检查添加 DDL 和获取它的代码中更改索引。

添加代码:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    DropDownList ddl;

    if (e.Row.RowIndex == 1)
    {
        // ..... 
    }
}

获取代码:

GridViewRow row = dgData.Rows[1];
DropDownList ddl = (DropDownList)row.Cells[1].FindControl("ddlCol1");
于 2013-06-03T09:10:33.897 回答