Thursday, February 25, 2010

Header Cell with CheckBox in DataGridView

This is my version of a class that displays a check box in a DataGridView Header cell.
This is based on the code from http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/827907ea-c529-4254-9b15-2e6d571f5c5b

It assumes that the check box is located in the first DataGridView column. It allows for column caption and provides built-in check/uncheck functionality. Play with x and y parameters to place the checkbox where you want in the header cell.

Here is a usage example:

DataGridViewColumn column = new DataGridViewCheckBoxColumn();
CheckBox ckBoxMaster = CheckBoxInHeader.CreateCheckBoxInHeader(column, 12, 4, "     Select All");
DataGridView1.Controls.Add(ckBoxMaster);


The class listing:

  class CheckBoxInHeader
    {
    public static CheckBox CreateCheckBoxInHeader(DataGridViewColumn column, int x, int y, string caption)
    {
        CheckBoxInHeader cbInHeader = new CheckBoxInHeader();
        CheckBox ckBox = cbInHeader.CreateCheckBox(column, x, y, caption);
        ckBox.CheckedChanged += new EventHandler(cbInHeader.ckBox_CheckedChanged);
        return ckBox;
    }

    private CheckBox CreateCheckBox(DataGridViewColumn column, int x, int y, string caption)
    {
        CheckBox ckBox = new CheckBox();
        //Get the column header cell bounds          
        Rectangle rect = column.DataGridView.GetCellDisplayRectangle(0, -1, true);
        ckBox.Size = new Size(18, 18);
        //play with check box position
        rect.Offset(x, y);
        //Change the location of the CheckBox to make it stay on the header
        ckBox.Location = rect.Location;
        column.Name = caption;
        return ckBox;
    }

    private void ckBox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = sender as CheckBox;
        DataGridView dgv = (DataGridView)cb.Parent;

        foreach (DataGridViewRow row in dgv.Rows)
        {

            row.Cells[0].Value = cb.Checked;
        }
    }

Thursday, February 18, 2010

Double-Click to Close WinForm ListBox

If you have a ListBox on a WinForm, and you want the double-click on a selected item to close the form, you need to create a handler for the ListBox double click event.
In the handling method, use the following code:
 private void listBox1_DoubleClick(object sender, EventArgs e)
        {
        if (listBox1.SelectedItem != null)
        {
            listBox1.FindForm().DialogResult = DialogResult.OK;
        }
        }
or, you could use a generic version of the above code:
 ListBox listBox = sender as ListBox ;
       if (listBox.SelectedItem != null)
       {
       listBox.FindForm().DialogResult = DialogResult.OK;
       }

Monday, February 8, 2010

Saving Multiple Files with the Same Name

If you need to save multiple files with the same name in the same directory, a common technique is to construct a file name adding an incremental counter to it, e.g. myfilename(1).txt, myfilename(2).txt, etc.

The method below allows you to create such a file name. It requires 1 parameter: a full file path. The method returns a FileInfo type.
It attempts first to construct a FileInfo object based on a file path, and then checks if such a file already exists. If it does not, the FileInfo object is returned. Otherwise, it constructs a new file name and checks for its existence until such a name no longer exists.

 

private static FileInfo GetFileInfo(string filePath)

{

    FileInfo fInfo = new FileInfo(filePath);

    string fileName = Path.GetFileNameWithoutExtension(filePath);

    string dirName = fInfo.DirectoryName;

    int counter = 1;

 

 

    while (fInfo.Exists)

    {

        string extension = fInfo.Extension;

        string nameProper = fileName + "(" + (counter++) + ")";

        filePath = Path.Combine(dirName, nameProper + extension);

        fInfo = new FileInfo(filePath);

        nameProper = String.Empty;

    }

    return fInfo;

}

Tuesday, February 2, 2010

Have DataGridView Scroll Down Automatically

The trick to having a WinForm DataGridView scroll down automatically to the last row as you populate it is to use the DataGridView CurrentCell property. Just set it to a cell in the last added row.
The DataGridView also has CurrentRow property but that is read-only.