Fill the Static Datagrid View columns

Sha1395
Sha1395 used Ask the Experts™
on
Hello All,

I was trying to populate my my datagridview column.But am not sure how i have to fill the static column.am doing the same thing in Listview it works fine but when i replace with datagridview am not sure where i missed.

Here is my code so far

 private void button9_Click(object sender, EventArgs e)
        {
            //int n = dataGridView1.Rows.Add();

            //dataGridView1.Rows[n].Cells[0].Value = "title";
            System.Messaging.Message mes;
            string m;
            int n = dataGridView1.Rows.Add();
          
            try
            {
               
                
                int len = mq.GetAllMessages().Length;
                
                if (len != 0)
                {
                    for (int i = 0; i < len; i++)
                    {
                        mes = mq.Receive(new TimeSpan(0, 0, 3));
                        mes.Formatter = new XmlMessageFormatter(new string[] { "System.String,mscorlib" });
                        m = mes.Body.ToString();
                    // listView1.Items.Add(new ListViewItem(new string[] {"1" ,"1234","TrainList","CRS" ,m.ToString(), }));
//Am trying to do the samething for my datagridview.
                       dataGridView1.Rows[n].Cells[0].Value = m.ToString();
                        //   listBox1.Items.Add(m.ToString());

                    }
                }
                else
                {
                    //listView1.Items.Clear();
                    dataGridView1.ClearSelection();
                    m = "No Error Queue Message";
                    //listView1.Items.Add(m.ToString());
                    dataGridView1.Rows[n].Cells[0].Value = m.ToString();
                    
                    
                }

            }
            catch
            {
                m = "no Message";
                //listView1.Items.Add(m.ToString());
                dataGridView1.Rows[n].Cells[0].Value = m.ToString();
            }
            

Open in new window


What am trying to acheive here is am getting Queue from my MSMQ and popluate in the grid so far am getting only one but its not looping.


Datagridview2Acheive.png
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Meir RivkinFull stack Software Engineer

Commented:
check the code

//instead of this line:
dataGridView1.Rows[n].Cells[0].Value = m.ToString();

//use this line
dataGridView1.Rows[i].Cells[0].Value = m.ToString();

Open in new window

Author

Commented:
Even i change the code like below still having problem.

    for (int i = 0; i < len; i++)
                    {
                        int j = 1;
                        j = j + i;

                        mes = mq.Receive(new TimeSpan(0, 0, 3));
                        mes.Formatter = new XmlMessageFormatter(new string[] { "System.String,mscorlib" });
                        m = mes.Body.ToString();
                        int n = dataGridView1.Rows.Add();
                       dataGridView1.Rows[j].Cells[4].Value = m.ToString();
                        //   listBox1.Items.Add(m.ToString());

                    }
               

Author

Commented:
Finally this code works and get all the data in the queue but am not sure why it erase the queue in msmq.

 if (len != 0)
                {
                    for (int i = 0; i < len; i++)
                    {
                        mes = mq.Receive(new TimeSpan(0, 0, 3));
                        mes.Formatter = new XmlMessageFormatter(new string[] { "System.String,mscorlib" });
                        m = mes.Body.ToString();
                        listView1.Items.Add(new ListViewItem(new string[] {"1" ,"1234","TrainList","CRS" ,m.ToString(), }));
                        int n = dataGridView1.Rows.Add();
                        int j = 1;
                        j = j + i;
                        dataGridView1.Rows[n].Cells[0].Value = j.ToString();
                        dataGridView1.Rows[n].Cells[1].Value = "12345";
                        dataGridView1.Rows[n].Cells[2].Value = "QRS";
                        dataGridView1.Rows[n].Cells[3].Value = "TrainList";
                        dataGridView1.Rows[n].Cells[4].Value = m.ToString();
                        //   listBox1.Items.Add(m.ToString());

                    }
                }
                else                             ///////////////////i just want to print "constant message like "No Queues are available"  and refresh every time the button clicks.
                {
                    listView1.Items.Clear();
                    m = "No Error Queue Message";
 dataGridView1.Rows[n].Cells[0].Value =m.tostring.
                 //   listView1.Items.Add(m.ToString());
                    //listView1.Items.Add(new ListViewItem(new string[] { "1", m.ToString() }));
                    
                }

            }

Open in new window

Full stack Software Engineer
Commented:
every time u call Receive, you pop out the first message in the queue (removing it from the queue)
check http://msdn.microsoft.com/en-us/library/system.messaging.messagequeue.receive.aspx

to return the message without removing it from the queue, use Peek:
http://msdn.microsoft.com/en-us/library/t5te2tk0.aspx

Author

Commented:
hi sedgwick

Peek will return the top queue only,i want to popluate all the queue in the msmq

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial