-->

C# Read Data From Database

This code lets you read data from the database. By default, MS SQL is mainly used in tutorials regarding .NET. This code connects to a MySQL database. If you are using MS SQL, just rename the classes that starts with MySQL to SQL. This code assumes that you already have a connection object in hand.

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MySqlConnection con = ...;
try {
  MySqlCommand cmd = con.createCommand();
  cmd.CommandText = "select username from users where username = 'me'";
  con.Open();
 
  MySqlDataReader dr = cmd.executeReader();
  if (dr.Read()) {
    Console.WriteLine("username = " + dr["username"]);
  }
} catch (Exception e) {
} finally {
  if (con != null) {
    con.Close();
    con.Dispose();
  }
}

Found this post useful? Donations appreciated. Every little $ helps.

C#: Add Select All, Deselect All Checkbox In Column Header In DataGridView Control


Adding controls to your DataGridView like a checkbox as a column is possible with the use of the DataGridViewCheckboxColumn class. This post assumes that you are using the DataGridView control in a Windows.Form application and that your gridview object can already populate data into it. The purpose of this post is to add a column containing checkboxes including a header checkbox, used to select and deselect all in one click.

The name of our DataGridView control is list. To add a checkbox column into the gridview control, use the following code below inside your className_Load() method.

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// customize dataviewgrid, add checkbox column
DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
checkboxColumn.Width = 30;
checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
list.Columns.Insert(0, checkboxColumn);
 
// add checkbox header
Rectangle rect = list.GetCellDisplayRectangle(0, -1, true);
// set checkbox header to center of header cell. +1 pixel to position correctly.
rect.X = rect.Location.X + (rect.Width / 4);
 
CheckBox checkboxHeader = new CheckBox();
checkboxHeader.Name = "checkboxHeader";
checkboxHeader.Size = new Size(18, 18);
checkboxHeader.Location = rect.Location;
checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
 
list.Controls.Add(checkboxHeader);

The method that does the selecting and deselecting of all checkboxes within the checkbox column is made possible with the CheckedChanged event. We add an event handler for this to make it work.

?View Code CSHARP
1
2
3
4
5
6
private void checkboxHeader_CheckedChanged(object sender, EventArgs e) {
    for (int i = 0; i < list.RowCount; i++) {
	list[0, i].Value = ((CheckBox) list.Controls.Find("checkboxHeader", true)[0]).Checked;
    }
    list.EndEdit();
}

That’s all there is to it. If you may wonder how to make the checkbox header align to the center, the code that makes that happen is the part where there is a Rectangle object used.

?View Code CSHARP
1
2
3
4
// add checkbox header
Rectangle rect = list.GetCellDisplayRectangle(0, -1, true);
// set checkbox header to center of header cell. +1 pixel to position correctly.
rect.X = rect.Location.X + (rect.Width4);

I divided the cell area’s width by 4 instead of 2 because I had to take into account the checkbox header’s width value. By the way, you also need to set the DataGridView control’s AutoSizeColumnsMode property to Fill so that all any empty spaces within the DataGridView control will be occupied

Found this post useful? Donations appreciated. Every little $ helps.

C#: Passing By Reference

Even if you know Object Oriented Programming in certain languages like Java, learning C# is totally different. One of the features that varies greatly in C# from Java is passing values by reference as parameters of methods. In Java, data types are automatically passed by value while classes are passed by reference. Good thing I decided to read out a beginner’s book for C#.

In C#, if you want to pass objects by reference, you would have to use the keyword ref. Take for example the sample code below

?View Code CSHARP
1
2
3
4
5
string str = "haha";
 
public void change(string param) {
	param = "hoho";
}

If you would call the method change() like this

?View Code CSHARP
1
2
change(str);
Console.WriteLine(str);

the output for the object str would be haha instead of hoho. You would have to use the ref keyword for this to work

?View Code CSHARP
1
2
3
public void change(ref string param) {
	param = "hoho";
}

Data types are a different story. To do the same way to data types as parameters in methods, you would use the out keyword like this

?View Code CSHARP
1
2
3
4
5
int i = 1;
 
public void change(out int param)_ {
	param = 10;
}

Calling the change method

?View Code CSHARP
1
2
change(i);
Console.WriteLine(i);

will output 10 as the value. If you do not use the out keyword, the value of i will still be 1.

Found this post useful? Donations appreciated. Every little $ helps.

Related Posts with Thumbnails