How to > DataGridView > Technical Logic >

.NET programming topics
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

How to > DataGridView > Technical Logic >

Post by Neofriend » Fri Jan 01, 2010 1:52 pm

Hey Friends,

Am pretty new to C# though I don't want that to discourage me.

I have a situation where am using DataGridView and am having a column in my database table namely "msg_status" which has either a value of "0" or "1". The DataGridView should make the rows bold if "msg_status" value is "1" and rest of the rows with value "0" of "msg_status" should be normal.

With that, I don't want "msg_status" to appear in DataGridView. And am only using "msg_status" in database for DataGridView to have something to decide either to show the row in bold or normal. More like generating the feeling of a read/unread email.

My friends, I've searched a lot on internet and came up with these two ideas people had in mind about how to implement...here http://www.c-sharpcorner.com/Forums/Sho ... adID=63866 but it doesn't help in how to make connection with the database and a lot of unanswered issues.

Please help with this, thank you in advance.
Two high hopes here... Neo and Cyclops :)
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 » Fri Jan 01, 2010 8:19 pm

Basically the link you posted explains how to highlight a row/column based on a cell. Following code will help you to connect to database and display a table on the grid.

Code: Select all

private void Form1_Load(object sender, EventArgs e){

    string connectionString = GetConnectionString();
    SqlConnection con = new SqlConnection(connectionString);
    string sqlSelect = "Select * from authors";
    SqlCommand cmd = new SqlCommand(sqlSelect,con);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    adp.Fill(dt);
    dataGridView1.DataSource = dt;
}  
 
static private string GetConnectionString(){

    return "Data Source=localhost; Initial Catalog = pubs; Integrated Security=SSPI";     // SQL Server
    //return "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\myDatabase.mdb";  // Access
}

Also I prefer following code for highlighting.

Code: Select all

foreach (DataGridViewRow r in dataGridView1.Rows){

    if ( (Convert.ToInt32(r.Cells[1].Value) == 0)){
        //Set Row Font to Bold
        r.DefaultCellStyle.Font = new Font("Arial", 12, FontStyle.Bold);
    }
}
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to > DataGridView > Technical Logic >

Post by Neofriend » Fri Jan 01, 2010 8:38 pm

Great, can you help with how to implement a solution, please and show an example with a sample database, having a simple table with a few rows and combine all the code to make an example doing what many of us would like to see.

Basically, a datagridview getting data from database (the easy part) but making the rows bold and normal based on content from database, and if we utilize a column for that decision then it shouldn't be displayed in the datagridview.

Sounds doable?

Thanks :)
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 » Fri Jan 01, 2010 8:54 pm

Hey, I can only give you guidance. Not coding :D
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: How to > DataGridView > Technical Logic >

Post by Neofriend » Fri Jan 01, 2010 8:58 pm

Alright, sorry for asking that favor.
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 » Fri Jan 01, 2010 9:00 pm

Okay..okay...I'll try to get you something :mrgreen:
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 » Fri Jan 01, 2010 9:31 pm

Here I made a simple app with VS.net 2005. I'm sorry I don't have 2008 with me but hope you could open it with 2008.
DataGridView.zip
(45.65 KiB) Downloaded 293 times
Note1: place the MyDB.mdb on the root of C: drive. (i.e.: C:\MyDB.mdb)
Note2: Add exception handling as required
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 12:40 am

Getting this exception namely The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine.

Besides, I changed the path from C: to a sub folder in C:

As windows 7 (x64 bit) isn't letting me put direct files for some security reasons. Though I changed the path in the code too.

Two things, it would be nice if you can show the example using sql server. Secondly, let me know the reason of this error please. So may be I can correct it here.

Thank you Neo.
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:01 am

SQL Server version:
DataGridViewEx.zip
(31.71 KiB) Downloaded 292 times
Make sure you adjust the following line with your database settings.

connString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
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 1:16 am

Nice.

Unfortunately, I always used to connect to SQL with Windows Authentication and have forgot the SQL Authentication password. The username is sa but the password is not ......

What to do?
Post Reply

Return to “.NET Programming”