Text Delete Column

9. Double-click the Add Column Button to create the button-click event handler and to open the form in code view.

10. Add the following code to Form1, which adds some additional code to the Form1_Load event that creates a new column on the DataTable and the code to add a new column to the DataGridView in the AddColumnButton_Click event.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load

'TODO: This line of code loads data into the 'NorthwindDataSet.Customers' table. You can move, or remove it, As needed.

Me.CustomersTableAdapter.Fill(Me.NorthwindDataSet.Customers)

' Add a new column to the Customers DataTable ' to be used to demonstrate adding and removing ' columns in a DataGridView in the methods below Dim Location As New DataCo1umn("Location") Location.Expression = "City + ', ' + Country" NorthwindDataSet.Customers.Columns.Add(Location) End Sub

Private Sub AddCo1umnButton_C1ick(ByVa1 sender As System.Object, ByVal e As System.EventArgs) _

Handles AddColumnButton.Click

Dim LocationColumn As New DataGridViewTextBoxColumn LocationColumn.Name = "LocationColumn" LocationColumn.HeaderText = "Location" LocationColumn.DataPropertyName = "Location" CustomersDataGridView.Columns.Add(LocationColumn) End Sub

private void Form1_Load(object sender, EventArgs e) {

// TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.

this.customersTableAdapter.Fill(this.northwindDataSet.Customers); // Add a new column to the Customers DataTable // to be used to demonstrate adding and removing // columns in a DataGridView in the methods below DataColumn Location = new DataColumn("Location"); Location.Expression = "City + ', ' + Country"; northwindDataSet.Customers.Columns.Add(Location);

private void AddColumnButton_Click(object sender, EventArgs e) {

DataGridViewTextBoxColumn LocationColumn = new DataGridViewTextBoxColumn(); LocationColumn.Name = "LocationColumn"; LocationColumn.HeaderText = "Location"; LocationColumn.DataPropertyName = "Location"; customersDataGridView.Columns.Add(LocationColumn);

11. Double-click the Delete Column Button to create the DeleteColumnButton_Click event handler. Add the following code to the DeleteColumnButton_Click event handler:

CustomersDataGridView.Columns.Remove("LocationColumn") Catch ex As Exception

MessageBox.Show(ex.Message) End Try

customersDataGridView.Columns.Remove("LocationColumn") ;

catch (Exception ex) {

MessageBox.Show(ex.Message);

12. Drag another button onto the form and set the following properties:

Was this article helpful?

0 0

Post a comment