0

Hi guys,

I'm new the this forum where I'm hoping to get some friendly advice for a bit-of-a-newbie C# programmer. I've been through 2 differnt beginners courses on the code so now i'm ready to get my hands in the code!

I'm currently having a problem with loading all data from Columns into a list view I made. The SQL Database was being read fine and eahc line was being pulled into 3 seperate text boxes but I decided I wanted it all in a list view.

However, I'm not to sure how to alter my code to show this. Below I have attached my working code:

 private void NavigateRecords()
        {
            DataRow dRow = ds1.Tables["Workers"].Rows[inc];

            ListViewItem item = new ListViewItem(dRow.ItemArray.GetValue(1).ToString());
            item.SubItems.Add(dRow.ItemArray.GetValue(2).ToString());
            item.SubItems.Add(dRow.ItemArray.GetValue(3).ToString());

            listView1.Items.Add(item);

As you can see I know that the 'Get Value' is only pointing to 1 line in the table. Is there an easy was to modifying this code to pull through all the data from the 3 columns at once? Sorry its a real n00b question but I really want to crack this one.

Thanks for any help you can offer!

regards
Mark.

2
Contributors
5
Replies
8
Views
4 Years
Discussion Span
Last Post by sundog1
0

How about using a foreach loop?

foreach (DataRow row in table.Rows)
{
    item.SubItems.Add(row["name of the column of that row"].ToString());
}

Hope this answer your question.

0

Hi wen,

Thanks for your response. I have tried that but I canot get it to work.. Below I have posted more of the code to show people what I'm trying to do:

       private void Form1_Load(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            ds1 = new DataSet();

            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";



            string sql = "SELECT * FROM tblWorkers";
            da = new System.Data.SqlClient.SqlDataAdapter(sql, con);

            con.Open();

            da.Fill(ds1, "Workers");
            NavigateRecords();
            MaxRows = ds1.Tables["Workers"].Rows.Count;

            con.Close();




        }

        private void NavigateRecords()
        {

            DataRow dRow = ds1.Tables["Workers"].Rows[inc];



            ListViewItem item = new ListViewItem(dRow["first_Name"].ToString());

            item.SubItems.Add(dRow["last_Name"].ToString());
            item.SubItems.Add(dRow["job_Title"].ToString());

            listView1.Items.Add(item);

            //textBox1.Text = dRow.ItemArray.GetValue(1).ToString();
            //textBox2.Text = dRow.ItemArray.GetValue(2).ToString();
            //textBox3.Text = dRow.ItemArray.GetValue(3).ToString();

        }

I welcome any help available.

Kind regards
Mark

0

Good morning, Anyone up this bright and early that can offer some advice on where to put the 'foreach' looping statement?

0

Hi Mark,

Just trying to clear the air. I was wondering if you have an error or could mention what seems to be the problem? Can you retrieve the information (like display the information on a textbox)? Because if you can, then I can assume your database connection is alright, and all you need to do is to assign the value into a list using a loop.

If I were to give an answer now, I think you should add the for loop starting from line 28. Hope to hear from you soon.

Regards,
Wen

0

Good morning Wen, Thank you for your reply.

Please note, that I was able to fix it.. I used the following code:

       private void NavigateRecords()
        {

            DataTable dtable = ds1.Tables["Workers"];


            for (int i = 0; i < dtable.Rows.Count; i++)
            {

                DataRow dRow = ds1.Tables["Workers"].Rows[inc];



                ListViewItem item = new ListViewItem(dRow["first_Name"].ToString());

                item.SubItems.Add(dRow["last_Name"].ToString());
                item.SubItems.Add(dRow["job_Title"].ToString());

                listView1.Items.Add(item);

            }

            //textBox1.Text = dRow.ItemArray.GetValue(1).ToString();
            //textBox2.Text = dRow.ItemArray.GetValue(2).ToString();
            //textBox3.Text = dRow.ItemArray.GetValue(3).ToString();

        }

I needed to add ' DataTable dtable = ds1.Tables["Workers"];' and then...

for (int i = 0; i < dtable.Rows.Count; i++)

thanks for your help guys.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.