4

这个问题其实是两个问题。我需要生成一个网格视图,其中一列是下拉选择。

我有以下代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ProjectsODS" OnRowUpdated="GridView1_RowUpdated" DataKeyNames="Id"
    CssClass="mGrid"
    PagerStyle-CssClass="pgr"
    AlternatingRowStyle-CssClass="alt" ShowHeaderWhenEmpty="True" OnRowEditing="GridView1_RowEditing">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Project Name" />
        <asp:BoundField DataField="ProjectCode" HeaderText="Code" />
        <asp:TemplateField HeaderText="Access">
            <ItemTemplate>
                <asp:DropDownList runat="server" AutoPostBack="True">
                    <asp:ListItem
                        Enabled="True"
                        Text="No Access"
                        Value="Test" />
                    <asp:ListItem
                        Enabled="True"
                        Text="Read Only"
                        Value="Tes 2" />
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>

    </Columns>
</asp:GridView>

最后一列确实是一个下拉列表。但是,目前,我正在对其中的项目进行硬编码,以查看它将如何呈现。我需要实际绑定来自另一个 ObjectDataSource 的值。但是对于网格中的每一行,它是相同的数据。它会命中每一行的 ODS,还是将数据读入 ODS 一次,并由每一行使用?

如何将 ODS 链接到 DropDownLists?

那么,如何将选定的值设置为行中的值?也就是说,生成gridview 的数据源有一个名为“AccessTypeId”的字段。我需要使用该值来选择 DDL 的值。我该怎么做?

然后,我将 AutoPostBack 设置为 true。一旦 DDL 获得用户设置的新值,我希望它发布该值。但是,当 DDL 选择的值改变时,即使在 Grid 上也会调用什么?

4

3 回答 3

1

有一些内置函数将帮助您完成所有这些工作。

1)关于从数据库每行加载元素,这不会发生你只能加载一次数据并将其插入DataTable并将每个网格行ddl的Datasource设置为该DataTable

如何? 有一个名为 OnRowDataBound 的函数,在 gridview 中插入的每一行都将转到此函数,您可以在此处找到下拉列表并从数据表中设置其列表项。

DataTable dt_Category = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStringDb1"].ToString()))
    {
        try
        {
            String cmdText = "SELECT CategoryName FROM Category";
            SqlCommand cmd = new SqlCommand(cmdText, cn);
            //cmd.Parameters.AddWithValue("@IsDeleted", "false");
            cn.Open();
            SqlDataAdapter myAdapter = new SqlDataAdapter(cmd);
            myAdapter.Fill(dt_Category);
            cn.Close();

            GridView1.DataSource = dt_Category;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
        }
    }
}

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Add you data here
        DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
        //Bind Data           
    }
}

2)关于获取选定的值,还有像 selectedindexchanged 这样的内置函数会为您获取选定的索引,并可以使用它来查找该行的下拉列表,如 row.FindControl("")

于 2013-08-10T12:37:11.683 回答
1

要将您的 objectDataSource 链接到 DropDownList,您可以在 Markup 中执行此操作:

<asp:TemplateField HeaderText="XYZ">
  <ItemTemplate>
    <asp:DropDownList runat="server" ID="MyDD" DataSourceId="MyDataSource" />
  </ItemTemplate> 
</asp:TemplateField>

如果您更喜欢动态绑定下拉列表,您可以在 RowDataBound 甚至在 RowCreated 事件中执行此操作,如下所示。这也回答了你的问题

how do I get the selected value set to be a value from the row? That is, the 
data source that  produces the gridview has a field called "AccessTypeId". 
I need that value to be used to select the value of the DDL. How would I do that

// 这里假设我们检索 CountryID 的当前行值。然后我们用这个特定 CountryID 的所有 States 值填充下拉列表。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings[1].ConnectionString;
        SqlConnection con = new SqlConnection(connectionString);
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            con.Open();
            var ddl = (DropDownList)e.Row.FindControl("ddlState");
            int CountryId = Convert.ToInt32(e.Row.Cells[0].Text);
            SqlCommand cmd = new SqlCommand("select * from State where CountryID=" + CountryId, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            con.Close();
            ddl.DataSource = ds;
            ddl.DataTextField = "StateName";
            ddl.DataValueField = "StateID";
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("--Select--", "0"));
        }
    }

现在,有趣的部分来了,how to get the selected value in DropDownlist to update the database. 基本上你的问题:

I have set AutoPostBack to true. Once the DDL gets a new value set by the user, 
I want it to post the value.

您应该使用 gridView 的 Update 事件来传递从下拉列表中选择的新值。您需要首先处于编辑模式,然后,从 DropDownList 中选择新值,单击更新按钮并处理 GridView 的 RowUpdating 事件。

我正在使用的一个是我定义了我的自定义 GridView.RowUpdating 事件,如下所示。您应该使用此事件来传递您新选择的DropDownList值。

 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {

  DropDownList ddl= (DropDownList )GridView1.Rows[e.RowIndex].FindControl("ddlState");
  string selectedvalue=ddl.selectedvalue;
  //My custom code to change last moment values with that selected from DropDownList 
  e.NewValues.Add("State", selectedvalue);

 }

的问题:

But, what even on the Grid is called when the DDL selected value is changed

如果您已将页面设置EnableViewStatefalse,则将为每个回发调用 ObjectDataSource Select 方法,因此也会像 OnRowDataBound 一样调用 GridView 事件。如果EnableViewStatetrue,则 Select 方法将只被调用一次,因此 GridView 事件。请参阅此堆栈问题:DataBind and Postback

[对于上述情况,我已经检查了 GridView 的 RowDataBound 事件]

于 2013-08-10T14:39:45.660 回答
0

1- 查询数据表作为 DropDownList 源

2-绑定 GridView

3- 在 RowsDataBound 上,做这样的事情

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Find the DropDownList in the Row

        DropDownList DropDown = (e.Row.FindControl("DropDown") as DropDownList);
        DropDown.DataSource = TheDatatable;
        DropDown.DisplayMember = "Name";
        DropDown.ValueMember = "ID";
        DropDown.DataBind();

        DropDown.SelectedIndexChanged += new EventHandler(DDL_SelectedIndexChanged);
    }
}

protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
{
    value = ((DropDownList)sender).SelectedValue;
}

注意添加下拉列表的 SelectedIndexChanged 事件处理程序

于 2013-08-10T12:51:12.373 回答