locked
Repeater column names from database RRS feed

  • Question

  • User1182587605 posted

    Hi,

    In my page, I have a Repeater and in that, I need to place column names by taking column names from a data table. Please help me with a code sample on how to do this. 

    Thanks,

    Deepak

    Thursday, June 8, 2017 9:43 AM

All replies

  • User475983607 posted

    If you are using a data set or data table to populate the Repeater then simply iterate over the columns collection to retrieve the name.

    List<string> columnNames = List<string>();
    foreach(DataColumn column in ds.Tables[0].Columns) 
    { 
        columnNames(column.ColumnName); 
    } 

    If you are using the SqldataReader then...

    List<string> columnNames = List<string>();
    for(int i=0;i<reader.FieldCount;i++)
    {
       columnNames.Add(reader.GetName(i));
    }

    The ASP Repeater is a very open control that allow the developer to write just about any kind of HTML.  So, that part is up to you.  If you are rendering a table then the column headers would be outside the Repeater.

    Reference documentation for the SqlDataReader and DataTable.  

    https://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.110).aspx

    https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx

    Thursday, June 8, 2017 10:48 AM
  • User-271186128 posted

    Hi Deepak,

    Do you mean you want to dynamically add header in repeater control? If that is the case, I suggest you could refer to the following code:

                <asp:Repeater ID="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
                    <HeaderTemplate>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <tr>
                                <td>
                                    <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'></asp:Label></td>
                                <td>
                                    <asp:Label runat="server" ID="lblName" Text='<%#Eval("Name") %>'></asp:Label></td>
                            </tr>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </tbody>
                       </table>
                    </FooterTemplate>
                </asp:Repeater>

    Code behind:

            protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[2] { new DataColumn("ID", typeof(string)), new DataColumn("Name", typeof(string)) });
                dt.Rows.Add("1001", "AAA");
                dt.Rows.Add("1002", "BBB");
                dt.Rows.Add("1003", "CCC");
    
                ViewState["DataTable"] = dt;
                Repeater1.DataSource = dt;
                Repeater1.DataBind();
            }
            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if(e.Item.ItemType == ListItemType.Header)
                {
                    //get table columns name.
                    DataTable dt = (DataTable)ViewState["DataTable"];
                    string[] columnNames = dt.Columns.Cast<DataColumn>()
                                     .Select(x => x.ColumnName)
                                     .ToArray();
                    //using literal to add table style.
                    Literal l1 = new Literal() { ID = "l1", Text = "<table><thead><tr><td>" };
                    e.Item.Controls.Add(l1);
    
                    //add header 
                    foreach(var item in columnNames)
                    {
                        Label lbl = new Label() { ID = "lbl" + item, Text = item };
                        e.Item.Controls.Add(lbl);
                        Literal l2 = new Literal() { ID = "l2", Text = "</td><td>" };
                        e.Item.Controls.Add(l2);
    
                    }
    
                    Literal l3 = new Literal() { ID = "l3", Text = "</td></tr><thead><tbody>" };
                    e.Item.Controls.Add(l3);
                }
            }

    The output as below:

    Best regards,
    Dillion

    Best regards,
    Dillion

    Friday, June 9, 2017 7:08 AM
  • User-1777561832 posted

    The code behind should not have Literal l2 added for the last item of the columnNames, it will add an extra dangling table column.

                //add header 
                string lastItem = columnNames[columnNames.Length - 1];
                foreach (var item in columnNames)
                {
                    Label lbl = new Label() { ID = "lbl" + item, Text = item };
                    e.Item.Controls.Add(lbl);
                    if (item != lastItem)
                    {
                        Literal l2 = new Literal() { ID = "l2", Text = "</th><th>" };
                        e.Item.Controls.Add(l2);
                    }
                }
    Thursday, July 18, 2019 3:52 AM
  • User-2054057000 posted

    It will be easier for you to take GridView in this situation. All you have to do is run the below query to fill the dataset/datatable:

    select * from table1

    And then bind the gridview to this dataset/datatable. 

    Note: You have to put the gridview's property autogeneratecolums=true.

    Sunday, July 28, 2019 2:18 PM