使用条件将行绑定到 gridview 中的下拉列表

Binding rows to a dropdown list in a gridview with a condition

数据是从出勤 table 中检索的,列名是状态 我想将员工的状态更改为 'leave' 如果员工的状态不是 'present'和 'absent'

我的aspx设计代码

<asp:DropDownList ID="ddlStatus" runat="server" DataValueField="<%# statusConversion(Eval("Status"))  %>">
                              <asp:ListItem value="Present" Text = "Present"></asp:ListItem>
                              <asp:ListItem value="Absent" Text="Absent"></asp:ListItem>
                              <asp:ListItem value="Leave" Text="Leave"></asp:ListItem>
                          </asp:DropDownList>ode here

我的C#后台函数代码

    public String statusConversion(object myVal)
        {
            String p = "Present";
            String a = "Absent";
            String l = "Leave";
            String val = myVal.ToString();
            if (val.Equals(a) || val.Equals(p))
            {
                return val;
            }
            else
            {

                val = l;
                return val;

            }
        }

根据你的描述,我建议你可以使用gridview row databound方法来实现你的要求。您可以在此方法中设置下拉菜单的 select 值,并使用您的自定义方法检查 stauts 代码。

更多详情,您可以参考以下代码:

ASPX:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewWithDDL.aspx.cs" Inherits="WebFormIdentityTest.GridviewWithDDL" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
                        <asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
                runat="server" AutoGenerateColumns="false" OnDataBound="GridView1_DataBound" >
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="Id" ItemStyle-Width="30" />

                    <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Operation">
                        <ItemTemplate>

                            <asp:HiddenField ID="HiddenField1" runat="server"  Value='<%# Eval("Status") %>'/>
                      <asp:DropDownList ID="ddlStatus" runat="server"  >                              
                           <asp:ListItem value="Present" Text = "Present"></asp:ListItem>
                              <asp:ListItem value="Absent" Text="Absent"></asp:ListItem>
                              <asp:ListItem value="Leave" Text="Leave"></asp:ListItem>
                          </asp:DropDownList>
                        </ItemTemplate>  
                     </asp:TemplateField>

                </Columns>
            </asp:GridView>
        </div>
    </form>
</body>
</html>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebFormIdentityTest
{
    public partial class GridviewWithDDL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
        {
            DataTable d1 = new DataTable();
            d1.Columns.Add("id");
            d1.Columns.Add("Status");
            d1.Columns.Add("SelectSource");
            d1.Columns.Add("SourceName");
            d1.Rows.Add("1", "Present", "bbbb", "aaaaaaa");
            d1.Rows.Add("2", "Absent", "ccccc", "dddddd");
            d1.Rows.Add("3", "Leave", "dddd", "fffff");
            d1.Rows.Add("4", "aaaa", "eeee", "ffff");
            d1.Rows.Add("5", "Leave", "cccc", "asdasdas");
            d1.Rows.Add("6", "bbb", "werwer", "qweqwe");
            GridView1.DataSource = d1;
            GridView1.DataBind();

        }


        public static String statusConversion(object myVal)
        {
            String p = "Present";
            String a = "Absent";
            String l = "Leave";
            String val = myVal.ToString();
            if (val.Equals(a) || val.Equals(p))
            {
                return val;
            }
            else
            {

                val = l;
                return val;

            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            foreach (GridViewRow gvRow in GridView1.Rows)
            {
                if (gvRow.RowType == DataControlRowType.DataRow)
                {
                    DropDownList ddl = gvRow.FindControl("ddlStatus") as DropDownList;
                    HiddenField hf = gvRow.FindControl("HiddenField1") as HiddenField;


                    ddl.SelectedValue = statusConversion(hf.Value);
                }



            }
        }
    }
}

结果: