Listing 61 Dynamic Column Construction

lnitializeComponent(); // Get the data

CustomersTableAdapter adapter = new CustomersTableAdapter(); m_CustomersBindingSource. DataSource = adapter.GetData();

// Set up the grid columns m_CustomersGrid.AutoGenerateColumns = false;

int newCollndex = m_CustomersGrid.Columns.Add("CompanyName",

"Company Name"); m_CustomersGrid.Columns[newCollndex].DataPropertyName =

"CompanyName"; newCollndex = m_CustomersGrid.Columns.Add("ContactName",

"Contact Name"); m_CustomersGrid.Columns[newCollndex].DataPropertyName = "ContactName";

newCollndex = m_CustomersGrid.Columns.Add("Phone","Phone"); m_CustomersGrid.Columns[newCollndex].DataPropertyName = "Phone"; newCollndex = m_CustomersGrid.Columns.Add("Contact", "Contact");

// Subscribe events m_CustomersGrid.CellFormatting += OnCellFormatting; m_CustomersGrid.RowsAdded += OnRowsAdded;

// Data bind m_CustomersGrid. DataSource = m_CustomersBindingSource;

This code first retrieves the Customers table data using the table adapter'33eTData method. As discussed earlier in the book, the table adapter was created along with the typed data set when you added the data source to your project. It sets the returned data table as the data source for the binding source. The AutoGenerateColumns property is set to false, since the code programmatically populates the columns collection. Then four text box columns are added to the grid using the overload of the Add method on the Columns collection, which takes a column name and the header text. The first three columns are set up for data binding to the Customers table's CompanyName, ContactName, and Phone columns by setting the DataPropertyName property on the column after it is created. The fourth column is the unbound column and is simply created at this point through the call to the Add method. It will be populated later through events.

Finally, the events of interest are wired up to the methods that will handle them using delegate inference. Using this new C# feature, you don't have to explicitly create an instance of a delegate to subscribe a handler for an event. You just assign a method name that has the appropriate signature for the event's delegate type, and the compiler will generate the delegate instance for you. In Visual Basic, you use the AddHandler operator, which has always operated similarly.

When the data source is set on the grid and the grid is rendered, the grid iterates through the rows of the data source and adds a row to the grid for each data source row, setting the values of the bound column cells to the corresponding values in the data source. As each row is created, the RowsAdded event is fired. In addition, a series of events are fired for every cell in the row as it is created.

4. Add the following method as the handler for the CellFormatting event:

private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

if (e.Columnlndex == m_CustomersGrid.Columns["Contact"]. Index) {

e.FormattingApplied = true;

DataGridViewRow row = m_CustomersGrid.Rows[e.Rowlndex]; e.Value = string.Format("{0}: {1}", row.Cells["ContactName"]. Value, row.Cells["Phone"]. Value);

As previously mentioned, you can use theCellFormatting event if you are programmatically setting the displayed values for the cells. The event argument that is passed in to the CellFormatting event exposes a number of properties to let you know what cell is being rendered. You can use the Columnlndex to determine which column the event is being fired for. It is checked against the index of the Contact column using a lookup in the Columns collection.

Once it is determined that this is the column you want to supply a programmatic value for, you can obtain the actual row being populated using the Rowlndex property on the event argument. In this case, the code just concatenates the ContactName and Phone from the row to form a contact information string using the String.Format method, and sets that string as the value on the Contact column.

In other situations, you may use the CellFormatting event to do something like look up a value from another table, such as using a foreign key, and use the retrieved value as the displayed value in the unbound column. It also sets the FormattingApplied property on the event argument totrue. This is very important to do; it signals the grid that this column is being dynamically updated. If you fail to do this, you will get an infinite loop if you also set the column to have its size automatically determined, as discussed in a later section.

0 0

Post a comment