private void Form1_Load(object sender, System.EventArgs e) { #region create our fake _dataSet DataSet _dataSet = new DataSet ("CustomerSet"); DataTable dt1 = new DataTable ("customers"); DataTable dt2 = new DataTable ("orders"); DataTable dt3 = new DataTable ("addresses"); DataColumn column; // customer table column = new DataColumn ("CustomerID"); column.DataType = typeof (int); column.Unique = true; dt1.Columns.Add (column); column = new DataColumn ("CustomerName"); column.DataType = typeof (string); column.Unique = false; dt1.Columns.Add (column); // order table column = new DataColumn ("OrderID"); column.DataType = typeof (int); column.Unique = true; dt2.Columns.Add (column); column = new DataColumn ("ItemName"); column.DataType = typeof (string); column.Unique = false; dt2.Columns.Add (column); column = new DataColumn ("CustomerID"); column.DataType = typeof (int); column.Unique = false; dt2.Columns.Add (column); column = new DataColumn ("AddressID"); column.DataType = typeof (int); column.Unique = false; dt2.Columns.Add (column); // address table column = new DataColumn ("AddressID"); column.DataType = typeof (int); column.Unique = true; dt3.Columns.Add (column); column = new DataColumn ("AddressString"); column.DataType = typeof (string); column.Unique = false; dt3.Columns.Add (column); column = new DataColumn ("CustomerID"); column.DataType = typeof (int); column.Unique = false; dt3.Columns.Add (column); for (int i = 0; i < 10; i ++) { DataRow row = dt1.NewRow (); row["CustomerID"] = i; row["CustomerName"] = String.Format ("Customer Name #{0}", i); dt1.Rows.Add (row); } int ordernum = 0; for (int i = 0; i < 10; i ++) { for (int j = 0; j < 4; j ++) { DataRow row = dt2.NewRow (); row["OrderID"] = ordernum++; row["ItemName"] = String.Format ("Item order #{0}", j); row["CustomerID"] = 9-i; row["AddressID"] = j; dt2.Rows.Add (row); } } int addressid = 0; for (int i = 0; i < 4; i ++) { for (int j = 0; j < 4; j ++) { DataRow row = dt3.NewRow (); row["AddressID"] = addressid++; row["AddressString"] = String.Format ("Customer Address {0}", j); row["CustomerID"] = i; dt3.Rows.Add (row); } } _dataSet.Tables.Add (dt1); _dataSet.Tables.Add (dt2); _dataSet.Tables.Add (dt3); // _dataSet.Relations.Add ("Customer_Orders", dt1.Columns["CustomerID"], dt2.Columns["CustomerID"]); // _dataSet.Relations.Add ("Customer_Addresses", dt1.Columns["CustomerID"], dt3.Columns["CustomerID"]); // _dataSet.Relations.Add ("Address_Orders", dt3.Columns["AddressID"], dt2.Columns["AddressID"]); #endregion #region create tablestyle & add custom columnstyles // Create a table style that will hold the new column style // that we set and also tie it to our customer's table from our DB DataGridTableStyle tableStyle = new DataGridTableStyle(); tableStyle.MappingName = "customers"; // since the dataset has things like field name and number of columns, // we will use those to create new columnstyles for the columns in our DB table int numCols = _dataSet.Tables["customers"].Columns.Count; // //add an extra column at the end of our customers table // _dataSet.Tables["customers"].Columns.Add("Button"); DataGridTextBoxColumn aColumnTextColumn = null; DataGridButtonColumn buttonColStyle = null; DataGridTextButtonColumn textButtonColStyle = null; for(int i = 0; i < numCols; ++i) { //add 2 button columns if( i == numCols - 1) { if ( false ) { textButtonColStyle = new DataGridTextButtonColumn(i); //pass the column# textButtonColStyle.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName; textButtonColStyle.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName; //hookup our cellbutton handler... textButtonColStyle.CellButtonClicked += new DataGridCellButtonClickEventHandler(HandleCellButtonClick); tableStyle.GridColumnStyles.Add(textButtonColStyle); //hook the mouse handlers dataGrid1.MouseDown += new MouseEventHandler(textButtonColStyle.HandleMouseDown); dataGrid1.MouseUp += new MouseEventHandler(textButtonColStyle.HandleMouseUp); } else { buttonColStyle = new DataGridButtonColumn(i); //pass the column# buttonColStyle.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName; buttonColStyle.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName; //hookup our cellbutton handler... buttonColStyle.CellButtonClicked += new DataGridCellButtonClickEventHandler(HandleCellButtonClick); tableStyle.GridColumnStyles.Add(buttonColStyle); //hook the mouse handlers dataGrid1.MouseDown += new MouseEventHandler(buttonColStyle.HandleMouseDown); dataGrid1.MouseUp += new MouseEventHandler(buttonColStyle.HandleMouseUp); } } else { // add standard textbox columns for the other columns aColumnTextColumn = new DataGridTextBoxColumn(); aColumnTextColumn.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName; aColumnTextColumn.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName; tableStyle.GridColumnStyles.Add(aColumnTextColumn); } } // make the dataGrid use our new tablestyle and bind it to our table dataGrid1.TableStyles.Clear(); dataGrid1.TableStyles.Add(tableStyle); #endregion //bind the table to the datagrid dataGrid1.DataSource = _dataSet.Tables["customers"]; }
private void Form1_Load(object sender, System.EventArgs e) { #region create our fake _dataSet DataSet _dataSet = new DataSet("CustomerSet"); DataTable dt1 = new DataTable("customers"); DataTable dt2 = new DataTable("orders"); DataTable dt3 = new DataTable("addresses"); DataColumn column; // customer table column = new DataColumn("CustomerID"); column.DataType = typeof(int); column.Unique = true; dt1.Columns.Add(column); column = new DataColumn("CustomerName"); column.DataType = typeof(string); column.Unique = false; dt1.Columns.Add(column); // order table column = new DataColumn("OrderID"); column.DataType = typeof(int); column.Unique = true; dt2.Columns.Add(column); column = new DataColumn("ItemName"); column.DataType = typeof(string); column.Unique = false; dt2.Columns.Add(column); column = new DataColumn("CustomerID"); column.DataType = typeof(int); column.Unique = false; dt2.Columns.Add(column); column = new DataColumn("AddressID"); column.DataType = typeof(int); column.Unique = false; dt2.Columns.Add(column); // address table column = new DataColumn("AddressID"); column.DataType = typeof(int); column.Unique = true; dt3.Columns.Add(column); column = new DataColumn("AddressString"); column.DataType = typeof(string); column.Unique = false; dt3.Columns.Add(column); column = new DataColumn("CustomerID"); column.DataType = typeof(int); column.Unique = false; dt3.Columns.Add(column); for (int i = 0; i < 10; i++) { DataRow row = dt1.NewRow(); row["CustomerID"] = i; row["CustomerName"] = String.Format("Customer Name #{0}", i); dt1.Rows.Add(row); } int ordernum = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 4; j++) { DataRow row = dt2.NewRow(); row["OrderID"] = ordernum++; row["ItemName"] = String.Format("Item order #{0}", j); row["CustomerID"] = 9 - i; row["AddressID"] = j; dt2.Rows.Add(row); } } int addressid = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { DataRow row = dt3.NewRow(); row["AddressID"] = addressid++; row["AddressString"] = String.Format("Customer Address {0}", j); row["CustomerID"] = i; dt3.Rows.Add(row); } } _dataSet.Tables.Add(dt1); _dataSet.Tables.Add(dt2); _dataSet.Tables.Add(dt3); // _dataSet.Relations.Add ("Customer_Orders", dt1.Columns["CustomerID"], dt2.Columns["CustomerID"]); // _dataSet.Relations.Add ("Customer_Addresses", dt1.Columns["CustomerID"], dt3.Columns["CustomerID"]); // _dataSet.Relations.Add ("Address_Orders", dt3.Columns["AddressID"], dt2.Columns["AddressID"]); #endregion #region create tablestyle & add custom columnstyles // Create a table style that will hold the new column style // that we set and also tie it to our customer's table from our DB DataGridTableStyle tableStyle = new DataGridTableStyle(); tableStyle.MappingName = "customers"; // since the dataset has things like field name and number of columns, // we will use those to create new columnstyles for the columns in our DB table int numCols = _dataSet.Tables["customers"].Columns.Count; // //add an extra column at the end of our customers table // _dataSet.Tables["customers"].Columns.Add("Button"); DataGridTextBoxColumn aColumnTextColumn = null; DataGridButtonColumn buttonColStyle = null; DataGridTextButtonColumn textButtonColStyle = null; for (int i = 0; i < numCols; ++i) { //add 2 button columns if (i == numCols - 1) { if (false) { textButtonColStyle = new DataGridTextButtonColumn(i); //pass the column# textButtonColStyle.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName; textButtonColStyle.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName; //hookup our cellbutton handler... textButtonColStyle.CellButtonClicked += new DataGridCellButtonClickEventHandler(HandleCellButtonClick); tableStyle.GridColumnStyles.Add(textButtonColStyle); //hook the mouse handlers dataGrid1.MouseDown += new MouseEventHandler(textButtonColStyle.HandleMouseDown); dataGrid1.MouseUp += new MouseEventHandler(textButtonColStyle.HandleMouseUp); } else { buttonColStyle = new DataGridButtonColumn(i); //pass the column# buttonColStyle.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName; buttonColStyle.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName; //hookup our cellbutton handler... buttonColStyle.CellButtonClicked += new DataGridCellButtonClickEventHandler(HandleCellButtonClick); tableStyle.GridColumnStyles.Add(buttonColStyle); //hook the mouse handlers dataGrid1.MouseDown += new MouseEventHandler(buttonColStyle.HandleMouseDown); dataGrid1.MouseUp += new MouseEventHandler(buttonColStyle.HandleMouseUp); } } else { // add standard textbox columns for the other columns aColumnTextColumn = new DataGridTextBoxColumn(); aColumnTextColumn.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName; aColumnTextColumn.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName; tableStyle.GridColumnStyles.Add(aColumnTextColumn); } } // make the dataGrid use our new tablestyle and bind it to our table dataGrid1.TableStyles.Clear(); dataGrid1.TableStyles.Add(tableStyle); #endregion //bind the table to the datagrid dataGrid1.DataSource = _dataSet.Tables["customers"]; }