开发者

Binding Drop down in grid view programatically

开发者 https://www.devze.com 2023-03-26 11:58 出处:网络
<asp:GridView ID=\"gvStates\" AutoGenerateColumns=\"false\" Width=\"100%\" AllowSorting=\"true\" runat=\"server\" OnRowCreated=\"gvStates_RowCreated\"
<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]

I'm trying to bind the开发者_JAVA技巧 'ddRec' dropdown to the Recruiter field in the dataset. I tried DataTextField='<%#Bind("Recruiter")%>'

Edit:

Error: {"Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."}

Let me explain better sorry. I'm trying to set the value from the proc but the dropdown list itself is being populated from a query on the OnRowCreated Event I think this is my problem

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();
    }
}


You can't programmatically bind to the DataTextField field; the DataTextField identifies the field to display in the drop down, hence it has to be static and is not evaluated per row. Though, you can tap into the Grid's RowDataBound event, and programmably set the DataTextField property and bind data at that point.

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消