How to > DataGridView > Technical Logic >

.NET programming topics
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to > DataGridView > Technical Logic >

Post by Neo » Mon Jan 04, 2010 1:28 am

Try this connection string.

Code: Select all

"data source=sqlec; initial catalog=Northwind; integrated security=SSPI; persist security info=False; Trusted_Connection=Yes"
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to > DataGridView > Technical Logic >

Post by Neofriend » Mon Jan 04, 2010 8:31 pm

Okay, I can load the data now and the grid view is showing up.

But there is no highlighting of rows. I changed the query as per my database namely "select * from transaction_tbl"

and I have a msg_status field having 0 or 1, I've tried using both the code people shared at csharpcorner for converting the rows to bold.

It isn't helping though.

Please let me know what can I do....
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to > DataGridView > Technical Logic >

Post by Neo » Tue Jan 05, 2010 12:26 am

Post a screenshot of the grid with loaded data along with the code that you have under the "Go" button.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to > DataGridView > Technical Logic >

Post by Neofriend » Tue Jan 05, 2010 11:12 am

Code: Select all

private void button1_Click(object sender, EventArgs e)
        {


            for (int rowIndex = 0; rowIndex < dataGridView1.Rows.Count; rowIndex++)
            {
                if (dataGridView1.Rows[rowIndex].Cells["msg_status"].Value != null)
                {
                    if ((bool) dataGridView1.Rows[rowIndex].Cells["msg_status"].Value)
                    {
                        //read mail
                    }
                    else
                    {
                        //Unread Mail
                        dataGridView1.Rows[rowIndex].DefaultCellStyle.Font = new Font(Font.FontFamily, Font.Size,
                                                                                      FontStyle.Bold);
                    }
                }
            }


            //foreach (DataGridViewRow r in dataGridView1.Rows)
            //{

            //    if ((Convert.ToInt32(r.Cells[6].Value) == 1))
            //    {
            //        //Set Row Font to Bold
            //        r.DefaultCellStyle.Font = new Font("Arial", 12, FontStyle.Bold);
            //    }
            //}

        }
Last edited by Neofriend on Thu Jan 07, 2010 2:19 am, edited 1 time in total.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to > DataGridView > Technical Logic >

Post by Neo » Tue Jan 05, 2010 12:17 pm

It seems that you have changed the button code to something else. Can you use the one that I have tested with VS.2005 pls.

Code: Select all

            foreach (DataGridViewRow r in dataGridView1.Rows)
            {

                if ((Convert.ToInt32(r.Cells[3].Value) == 1)) // change 3 to the index of the selection field
                {
                    //Set Row Font to Bold
                    r.DefaultCellStyle.Font = new Font("Arial", 12, FontStyle.Bold);
                }
            }
Basically you just have to change the column index (in red colour) of line if ((Convert.ToInt32(r.Cells[3].Value) == 1)) to the field index of the table in grid.
Note that if you do not need the particular field to appear on the grid, you can hide that column from code.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to > DataGridView > Technical Logic >

Post by Neofriend » Tue Jan 05, 2010 12:42 pm

Yes, I first tried doing that. Exactly the same way you mentioned. But it didn't work.

Working at your end?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to > DataGridView > Technical Logic >

Post by Neo » Tue Jan 05, 2010 12:53 pm

Highlight.PNG
Highlight.PNG (13.21 KiB) Viewed 7660 times
Note that the index is starting from 0. If the filed is appearing on the 5th column, the index is 4.
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to > DataGridView > Technical Logic >

Post by Neofriend » Tue Jan 05, 2010 1:20 pm

Cool, its working now -
The mistake was... I was not pressing the button :) sorry...

Having some more questions regarding the same. But let's give this thread a bit of rest :)
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to > DataGridView > Technical Logic >

Post by Neofriend » Tue Jan 05, 2010 1:21 pm

But yes one important thing, how can we hide that status column from design view and code?
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to > DataGridView > Technical Logic >

Post by Neofriend » Tue Jan 05, 2010 1:36 pm

Alright, I've found out... its

dataGridView1.Columns[6].Visible = false;

Thank you :)

Doing more research and practice, you are of great help.
Post Reply

Return to “.NET Programming”