以编程方式绑定gridview中的下拉列表

时间:2011-08-05 23:05:10

标签: c# asp.net

<asp:GridView ID="gvStates" AutoGenerateColumns="false" Width="100%" AllowSorting="true" 
                  runat="server" OnRowCreated="gvStates_RowCreated" 
                  OnRowDataBound="gvStates_RowCreated">
        <HeaderStyle BackColor="#57768f" ForeColor="White" />
        <RowStyle BackColor="#dae2e8" ForeColor="Black" HorizontalAlign="Center" />
        <AlternatingRowStyle BackColor="#ffffff" ForeColor="Black" />
        <Columns>
            <asp:BoundField HeaderText="key" DataField="key" />
            <asp:BoundField HeaderText="Name" DataField="Name" />
            <asp:BoundField HeaderText="Quota" DataField="Quota" />
            <asp:BoundField HeaderText="Session" DataField="Sess" >
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlSess" Width="100%" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddl">
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Scheduled">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Bind("Sched")%>' Enabled="false" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#Bind("Sched")%>' />
                </EditItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:Button ID="_b_SchStat" runat="server" AutoPostBack="true" Text="UnSchedule" OnClick="_b_ToggleSched" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Recruiter">
                <ItemTemplate>
                    <asp:DropDownList ID="ddRec" Width="100%" AutoPostBack="true" runat="server" OnSelectedIndexChanged="ddR">
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:BoundField HeaderText="MN Phone" DataField="MN Phone" />
            <asp:BoundField HeaderText="Cell Phone" DataField="Cell Phone" />
        </Columns>
    </asp:GridView>

[dbo].[QRY_RecruitGrid]
@jobnum     varchar(20),
@quota          varchar(10),
@sess           VARCHAR(10)
AS
SELECT 
[job_resp_recordid] as 'key'
,[job_resp_name] as 'Name'
,[job_resp_quota] as 'Quota' 
,[job_resp_session] as 'Sess'
,[job_resp_scheduled] as 'Sched' 
,COALESCE([job_resp_recruited_by], '') as 'Recruiter'
,case when len(ltrim(rtrim([job_resp_phone])))='10' then '('+SUBSTRING([job_resp_phone],1,3)+')'+'      '+SUBSTRING([job_resp_phone],4,3)+'-'+SUBSTRING([job_resp_phone],7,4) when len(ltrim(rtrim([job_resp_phone])))='' then ' ' end AS [MN   Phone]
,case when len(ltrim(rtrim([job_resp_cellphone])))='10' then '('+SUBSTRING([job_resp_cellphone],1,3)+')'+'   '+SUBSTRING([job_resp_cellphone],4,3)+'-'+SUBSTRING([job_resp_cellphone],7,4) when len(ltrim(rtrim([job_resp_cellphone])))='' then ' '  end AS [Cell Phone] 
FROM [dbo].[tbl_job_respondents] 
WHERE job_resp_job_number like @jobnum 
and job_resp_quota like @quota 
AND job_resp_session LIKE @sess
order by job_resp_quota, [job_resp_name]

我正在尝试将'ddRec'下拉列表绑定到数据集中的Recruiter字段。我试过了     DataTextField = '&LT;%#绑定( “招聘”)%&GT;'

编辑:

错误:{“Eval(),XPath()和Bind()等数据绑定方法只能在数据绑定控件的上下文中使用。”}

让我解释一下对不起。我正在尝试设置proc的值,但下拉列表本身是从OnRowCreated事件的查询中填充的,我认为这是我的问题

protected void gvStates_RowCreated(object sender, GridViewRowEventArgs e)
{
var drop = new List<string> { "" };

    var LNQ = new LNQDataContext();
    var Rec = LNQ.Recruits.Where(c => c.Active == "Y").Select(c => new { c.Name });
    var Rdp = new List<string> { "" };
    foreach (var a in Rec) { Rdp.Add(a.Name); }

    for (int i = 1; i <= _cnt; i++) { drop.Add("S" + i); }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var ddl = (DropDownList)e.Row.FindControl("ddlSess");
        ddl.DataSource = drop;
        ddl.DataBind();

        var ddR = (DropDownList)e.Row.FindControl("ddRec");
        ddR.DataSource = Rdp;
        ddR.DataBind();
    }
}

1 个答案:

答案 0 :(得分:1)

您无法以编程方式绑定到DataTextField字段; DataTextField标识要在下拉列表中显示的字段,因此它必须是静态的,并且不会按行计算。但是,您可以使用Grid的RowDataBound事件,并可编程地设置DataTextField属性并在该点绑定数据。

HTH。