none
Set Label Equal To DatTable Value RRS feed

  • Question

  • I have a DataTable with one column, name.


    It will hold between 1 and 10 names.  I have 10 labels, that are all invisible and are being set to visible based off the number of names in the DataTable.

    How can I iterate the DataTable and set label1.Text = row 1 of DataTable, label2.Text = row 2 of DataTable etc? 

    Friday, October 4, 2019 10:04 PM

Answers

  • The following assumes the label names are label1 . . . label10.

    using System;
    using System.Data;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace Demo1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // names
                var nameList = GetDataTable().AsEnumerable().Select((row, rowIndex) => 
                    new RowData {Index = rowIndex +1, Name = row.Field<string>("FirstName")}).ToList();
    
                // labels
                var labelList = Controls.Cast<Control>()
                    .Where(c => c.GetType() == typeof(Label)).ToList();
    
                Control currentControl = null;
    
                for (int rowIndex = 0; rowIndex < nameList.Count; rowIndex++)
                {
                    currentControl = labelList.FirstOrDefault(lb => lb.Name == $"label{rowIndex}");
    
                    if (currentControl == null) continue;
    
                    currentControl.Visible = true;
                    currentControl.Text = nameList[rowIndex - 1].Name;
                }
            }
    
            public DataTable GetDataTable()
            {
                var dt = new DataTable();
                dt.Columns.Add(new DataColumn() { ColumnName = "FirstName", DataType = typeof(string)});
    
                dt.Rows.Add("Karen");
                dt.Rows.Add("Jane");
                dt.Rows.Add("Mike");
                dt.Rows.Add("Sue");
                dt.Rows.Add("Sue 1");
                dt.Rows.Add("Sue 2");
                dt.Rows.Add("Sue 3");
                dt.Rows.Add("Sue 4");
                dt.Rows.Add("Sue 5");
                dt.Rows.Add("Bill");
                dt.Rows.Add("Will not see me");
                return dt;
            }
        }
    
        public class RowData
        {
            public int Index { get; set; }
            public string Name { get; set; }
        }
    }
    

    RowData class should be in it's own class file but will as you see works above.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by IndigoMontoya Saturday, October 5, 2019 12:03 AM
    Friday, October 4, 2019 11:34 PM
    Moderator