Home All Groups Group Topic Archive Search About

Populate DropDownList w/ GridView Header Text

Author
29 Nov 2006 5:32 PM
ccshine via DotNetMonster.com
I have a GridView bound with an SQLDataSource. Search functionality for the
GridView needs to be added through the use of a DropDownList containing all
of the visible fields from the GridView, in addition to a textbox for search
text.  I want to avoid hard coding the values in the DropDownList.  How can I
retrieve these values programmatically either from the SQLDataSource or the
GridView itself?

Thanks in advance.


Author
30 Nov 2006 12:34 AM
MikeS
After your bound, go after the cells. If you are using an auto data
source then catch the row created event for the header and do the same
thing.

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource =
System.Diagnostics.Process.GetCurrentProcess().Modules;
            GridView1.DataBind();
            foreach (TableCell tc in GridView1.HeaderRow.Cells)
            {
                DropDownList1.Items.Add(tc.Text);
            }
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="true">
        </asp:DropDownList>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>

    </div>
    </form>
</body>
</html>
Author
30 Nov 2006 4:00 PM
ccshine via DotNetMonster.com
That's kinda the direction I was going, but I inserted similar code in the
RowDataBound event.  I altered the code per your example and placed in the
the Page_Load event (If Not (IsPostBack)), but it's not working as expected.
The drop down now contains 2 instances of &nbsp;. which seem to be generated
from the hidden and Edit command fields.  I removed the hidden field as I
really didn't need it with the SQLDataSource and it now contains one &nbsp;.

Here's the code I used.

            For Each cell As TableCell In grdUsers.HeaderRow.Cells
                ddlField.Items.Add(cell.Text)
            Next

Author
30 Nov 2006 4:04 PM
ccshine via DotNetMonster.com
Just in case it has something to do with the GridView setup....

                                     <asp:GridView id="grdUsers"
runat="server" CssClass="lgGridview" CellPadding="3" AllowSorting="true"
AutoGenerateColumns="false" DataKeyNames="agentId"
                                        EditRowStyle-BackColor="#BDCBDE"
AutoGenerateEditButton="false" PagerSettings-
FirstPageImageUrl="~/Images/first.gif" PagerSettings-
LastPageImageUrl="~/Images/last.gif"  PagerSettings-
NextPageImageUrl="~/Images/next.gif" PagerSettings-
PreviousPageImageUrl="~/Images/previous.gif"
                                        PagerSettings-Position="TopAndBottom"
PagerSettings-Mode="NextPreviousFirstLast" PageSize="20" AllowPaging="true"
EnableSortingAndPagingCallbacks="true" DataSourceID="srcUsers">
                                            <Columns>
                                                <asp:CommandField
ShowEditButton="true" EditImageUrl="~/Images/edit.gif"
CancelImageUrl="~/Images/cancel.gif" UpdateImageUrl="~/Images/update.gif"
ButtonType="Image" />
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="lastName" SortExpression="lastName" HeaderText="Last"
HeaderStyle-HorizontalAlign="center" HeaderStyle-CssClass="lgGridviewHeader"
ItemStyle-CssClass="lgGridviewItem" ></asp:BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="firstName" SortExpression="firstName"
HeaderText="First" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="ntLogin" SortExpression="ntLogin" HeaderText="NT
Login" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="opsComEleVid" SortExpression="opsComEleVid"
HeaderText="opsComEleVid" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="opsComEleHsd" SortExpression="opsComEleHsd"
HeaderText="opsComEleHsd" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="agentSup" SortExpression="agentSup"
HeaderText="Supervisor" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="agentCsgOpid" SortExpression="agentCsgOpid"
HeaderText="agentCsgOpid" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="agentAspect" SortExpression="agentAspect"
HeaderText="Aspect" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="agentSalesNum" SortExpression="agentSalesNum"
HeaderText="Sales#" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="teamName" SortExpression="teamName" HeaderText="Team"
HeaderStyle-HorizontalAlign="center" HeaderStyle-CssClass="lgGridviewHeader"
ItemStyle-CssClass="lgGridviewItem" ></asp:BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="skillSet" SortExpression="skillSet" HeaderText="Skill
Set" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="caeTools" SortExpression="caeTools"
HeaderText="caeTools" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="nasTeamName"  SortExpression="nasTeamName"
HeaderText="nasTeamName" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-CssClass="lgGridviewItem" ></asp:
BoundField>
                                                <asp:BoundField HeaderStyle-
Wrap="false" DataField="carNum" SortExpression="carNum" HeaderText="carNum"
HeaderStyle-HorizontalAlign="center" HeaderStyle-CssClass="lgGridviewHeader"
ItemStyle-CssClass="lgGridviewItem" ></asp:BoundField>
                                                <asp:CheckBoxField
HeaderStyle-Wrap="false" DataField="active" SortExpression="active"
HeaderText="Active" HeaderStyle-HorizontalAlign="center" HeaderStyle-
CssClass="lgGridviewHeader" ItemStyle-HorizontalAlign="center" ></asp:
CheckBoxField>
                                            </Columns>

                                            </asp:GridView>


ccshine wrote:
>That's kinda the direction I was going, but I inserted similar code in the
>RowDataBound event.  I altered the code per your example and placed in the
>the Page_Load event (If Not (IsPostBack)), but it's not working as expected.
>The drop down now contains 2 instances of &nbsp;. which seem to be generated
>from the hidden and Edit command fields.  I removed the hidden field as I
>really didn't need it with the SQLDataSource and it now contains one &nbsp;.
>
>Here's the code I used.
>
>            For Each cell As TableCell In grdUsers.HeaderRow.Cells
>                ddlField.Items.Add(cell.Text)
>            Next

Author
1 Dec 2006 3:18 AM
MikeS
Perhaps don't include cell.Text if it is ="".