Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I show unbound data along with bound data?

The data you display in the DataGridView control will normally come from a data source of some kind, but you might want to display a column of data that does not come from the data source. This kind of column is called an unbound column. Unbound columns can take many forms. As discussed in the data section above, you can use virtual mode to display additional data along with bound data.

The following code example demonstrates how to create an unbound column of check box cells to enable the user to select database records to process. The grid is put into virtual mode and responds to the necessary events. The selected records are kept by ID in a dictionary to allow the user to sort the content but not lose the checked rows.

private System.Collections.Generic.Dictionary<int, bool> checkState;
private void Form1_Load(object sender, EventArgs e)
{
  dataGridView1.AutoGenerateColumns = false;
  dataGridView1.DataSource = customerOrdersBindingSource;
  // The check box column will be virtual.
  dataGridView1.VirtualMode = true;
  dataGridView1.Columns.Insert(0, new DataGridViewCheckBoxColumn());
  // Initialize the dictionary that contains the boolean check state.
  checkState = new Dictionary<int, bool>();
}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
  // Update the status bar when the cell value changes.
  if (e.ColumnIndex == 0 && e.RowIndex != -1)
  {
    // Get the orderID from the OrderID column.
    int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
    checkState[orderID] = (bool)dataGridView1.Rows[e.RowIndex].Cells[0].Value;
  }
}

private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
  // Handle the notification that the value for a cell in the virtual column
  // is needed. Get the value from the dictionary if the key exists.
  if (e.ColumnIndex == 0)
  {
    int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
    if (checkState.ContainsKey(orderID))
    {
      e.Value = checkState[orderID];
    }
    else
      e.Value = false;
  }
}
private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
{
  // Handle the notification that the value for a cell in the virtual column
  // needs to be pushed back to the dictionary.
  if (e.ColumnIndex == 0)
  {
    // Get the orderID from the OrderID column.
    int orderID = (int)dataGridView1.Rows[e.RowIndex].Cells["OrderID"].Value;
    // Add or update the checked value to the dictionary depending on if the 
    // key (orderID) already exists.
    if (!checkState.ContainsKey(orderID))
    {
      checkState.Add(orderID, (bool)e.Value);
    }
  else
    checkState[orderID] = (bool)e.Value;
  }
}