Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Mapping tablestyle's mappingname onto a dataview
Message
From
25/05/2005 02:45:57
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
24/05/2005 13:45:35
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
01017016
Message ID:
01017379
Views:
26
This message has been marked as the solution to the initial question of the thread.
>Hi Cetin,
>
>Thank you for your answer. I tried to manually add the dataview as the datagrid's datasource (like the example code you posted) but it seems like the tablestyle does not get applied. It works fine if I use a datatable as the datasource (the tablestyle is applied correctly) but it gets ignored when the datasource is a dataview.
>
>I use the dataview's name as the tablestyle's mappingname.
>
>Sincerely,
>
>Andrew

Andrew,
That's what I meant. Use datatable as source, not dataview. Think of them as 2 gloves with different functionalities and you wear them both (couldn't find a good analogy). Below is a sample (actually this sample is directly from online help, I only added 'Dataview' button and its click code):
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : System.Windows.Forms.Form
{
	private System.ComponentModel.Container components;
	private Button button1;
	private Button button2;
	private DataGrid myDataGrid;   
	private DataSet myDataSet;
	private System.Windows.Forms.Button button3;
	private bool TablesAlreadyAdded;
	public Form1()
	{
		// Required for Windows Form Designer support.
		InitializeComponent();
		// Call SetUp to bind the controls.
		SetUp();
	}

	protected override void Dispose( bool disposing )
	{
		if( disposing )
		{
			if (components != null)
			{
				components.Dispose();}
		}
		base.Dispose( disposing );
	}
	private void InitializeComponent()
	{
		this.button1 = new System.Windows.Forms.Button();
		this.button2 = new System.Windows.Forms.Button();
		this.myDataGrid = new System.Windows.Forms.DataGrid();
		this.button3 = new System.Windows.Forms.Button();
		((System.ComponentModel.ISupportInitialize)(this.myDataGrid)).BeginInit();
		this.SuspendLayout();
		// 
		// button1
		// 
		this.button1.Location = new System.Drawing.Point(24, 16);
		this.button1.Name = "button1";
		this.button1.Size = new System.Drawing.Size(120, 24);
		this.button1.TabIndex = 0;
		this.button1.Text = "Change Appearance";
		this.button1.Click += new System.EventHandler(this.button1_Click);
		// 
		// button2
		// 
		this.button2.Location = new System.Drawing.Point(150, 16);
		this.button2.Name = "button2";
		this.button2.Size = new System.Drawing.Size(120, 24);
		this.button2.TabIndex = 1;
		this.button2.Text = "Get Binding Manager";
		this.button2.Click += new System.EventHandler(this.button2_Click);
		// 
		// myDataGrid
		// 
		this.myDataGrid.CaptionText = "Microsoft DataGrid Control";
		this.myDataGrid.DataMember = "";
		this.myDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
		this.myDataGrid.Location = new System.Drawing.Point(24, 50);
		this.myDataGrid.Name = "myDataGrid";
		this.myDataGrid.Size = new System.Drawing.Size(300, 200);
		this.myDataGrid.TabIndex = 2;
		this.myDataGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Grid_MouseUp);
		// 
		// button3
		// 
		this.button3.Location = new System.Drawing.Point(288, 16);
		this.button3.Name = "button3";
		this.button3.TabIndex = 3;
		this.button3.Text = "DataView";
		this.button3.Click += new System.EventHandler(this.button3_Click);
		// 
		// Form1
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.ClientSize = new System.Drawing.Size(450, 330);
		this.Controls.Add(this.button3);
		this.Controls.Add(this.button1);
		this.Controls.Add(this.button2);
		this.Controls.Add(this.myDataGrid);
		this.Name = "Form1";
		this.Text = "DataGrid Control Sample";
		((System.ComponentModel.ISupportInitialize)(this.myDataGrid)).EndInit();
		this.ResumeLayout(false);

	}

	public static void Main()
	{
		Application.Run(new Form1());
	}
   
	private void SetUp()
	{
		// Create a DataSet with two tables and one relation.
		MakeDataSet();
		/* Bind the DataGrid to the DataSet. The dataMember
		specifies that the Customers table should be displayed.*/
		myDataGrid.SetDataBinding(myDataSet, "Customers");
	}

	protected void button1_Click(object sender, System.EventArgs e)
	{
		if(TablesAlreadyAdded) return;
		AddCustomDataTableStyle();
	}

	private void AddCustomDataTableStyle()
	{
		DataGridTableStyle ts1 = new DataGridTableStyle();
		ts1.MappingName = "Customers";
		// Set other properties.
		ts1.AlternatingBackColor = Color.LightGray;

		/* Add a GridColumnStyle and set its MappingName 
		to the name of a DataColumn in the DataTable. 
		Set the HeaderText and Width properties. */
      
		DataGridColumnStyle boolCol = new DataGridBoolColumn();
		boolCol.MappingName = "Current";
		boolCol.HeaderText = "IsCurrent Customer";
		boolCol.Width = 150;
		ts1.GridColumnStyles.Add(boolCol);
      
		// Add a second column style.
		DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
		TextCol.MappingName = "custName";
		TextCol.HeaderText = "Customer Name";
		TextCol.Width = 250;
		ts1.GridColumnStyles.Add(TextCol);

		// Create the second table style with columns.
		DataGridTableStyle ts2 = new DataGridTableStyle();
		ts2.MappingName = "Orders";

		// Set other properties.
		ts2.AlternatingBackColor = Color.LightBlue;
      
		// Create new ColumnStyle objects
		DataGridColumnStyle cOrderDate = 
			new DataGridTextBoxColumn();
		cOrderDate.MappingName = "OrderDate";
		cOrderDate.HeaderText = "Order Date";
		cOrderDate.Width = 100;
		ts2.GridColumnStyles.Add(cOrderDate);

		/* Use a PropertyDescriptor to create a formatted
		column. First get the PropertyDescriptorCollection
		for the data source and data member. */
		PropertyDescriptorCollection pcol = this.BindingContext
			[myDataSet, "Customers.custToOrders"].GetItemProperties();
 
		/* Create a formatted column using a PropertyDescriptor.
		The formatting character "c" specifies a currency format. */     
		DataGridColumnStyle csOrderAmount = 
			new DataGridTextBoxColumn(pcol["OrderAmount"], "c", true);
		csOrderAmount.MappingName = "OrderAmount";
		csOrderAmount.HeaderText = "Total";
		csOrderAmount.Width = 100;
		ts2.GridColumnStyles.Add(csOrderAmount);

		/* Add the DataGridTableStyle instances to 
		the GridTableStylesCollection. */
		myDataGrid.TableStyles.Add(ts1);
		myDataGrid.TableStyles.Add(ts2);

		// Sets the TablesAlreadyAdded to true so this doesn't happen again.
		TablesAlreadyAdded=true;
	}

	protected void button2_Click(object sender, System.EventArgs e)
	{
		BindingManagerBase bmGrid;
		bmGrid = BindingContext[myDataSet, "Customers"];
		MessageBox.Show("Current BindingManager Position: " + bmGrid.Position);
	}

	private void Grid_MouseUp(object sender, MouseEventArgs e)
	{
		// Create a HitTestInfo object using the HitTest method.

		// Get the DataGrid by casting sender.
		DataGrid myGrid = (DataGrid)sender;
		DataGrid.HitTestInfo myHitInfo = myGrid.HitTest(e.X, e.Y);
		Console.WriteLine(myHitInfo);
		Console.WriteLine(myHitInfo.Type);
		Console.WriteLine(myHitInfo.Row);
		Console.WriteLine(myHitInfo.Column);
	}

	// Create a DataSet with two tables and populate it.
	private void MakeDataSet()
	{
		// Create a DataSet.
		myDataSet = new DataSet("myDataSet");
      
		// Create two DataTables.
		DataTable tCust = new DataTable("Customers");
		DataTable tOrders = new DataTable("Orders");

		// Create two columns, and add them to the first table.
		DataColumn cCustID = new DataColumn("CustID", typeof(int));
		DataColumn cCustName = new DataColumn("CustName");
		DataColumn cCurrent = new DataColumn("Current", typeof(bool));
		tCust.Columns.Add(cCustID);
		tCust.Columns.Add(cCustName);
		tCust.Columns.Add(cCurrent);

		// Create three columns, and add them to the second table.
		DataColumn cID = 
			new DataColumn("CustID", typeof(int));
		DataColumn cOrderDate = 
			new DataColumn("orderDate",typeof(DateTime));
		DataColumn cOrderAmount = 
			new DataColumn("OrderAmount", typeof(decimal));
		tOrders.Columns.Add(cOrderAmount);
		tOrders.Columns.Add(cID);
		tOrders.Columns.Add(cOrderDate);

		// Add the tables to the DataSet.
		myDataSet.Tables.Add(tCust);
		myDataSet.Tables.Add(tOrders);

		// Create a DataRelation, and add it to the DataSet.
		DataRelation dr = new DataRelation
			("custToOrders", cCustID , cID);
		myDataSet.Relations.Add(dr);
   
		/* Populates the tables. For each customer and order, 
		creates two DataRow variables. */
		DataRow newRow1;
		DataRow newRow2;

		// Create three customers in the Customers Table.
		for(int i = 1; i < 4; i++)
		{
			newRow1 = tCust.NewRow();
			newRow1["custID"] = i;
			// Add the row to the Customers table.
			tCust.Rows.Add(newRow1);
		}
		// Give each customer a distinct name.
		tCust.Rows[0]["custName"] = "Customer1";
		tCust.Rows[1]["custName"] = "Customer2";
		tCust.Rows[2]["custName"] = "Customer3";

		// Give the Current column a value.
		tCust.Rows[0]["Current"] = true;
		tCust.Rows[1]["Current"] = true;
		tCust.Rows[2]["Current"] = false;

		// For each customer, create five rows in the Orders table.
		for(int i = 1; i < 4; i++)
		{
			for(int j = 1; j < 6; j++)
			{
				newRow2 = tOrders.NewRow();
				newRow2["CustID"]= i;
				newRow2["orderDate"]= new DateTime(2001, i, j * 2);
				newRow2["OrderAmount"] = i * 10 + j  * .1;
				// Add the row to the Orders table.
				tOrders.Rows.Add(newRow2);
			}
		}
	}

	private void button3_Click(object sender, System.EventArgs e)
	{
		DataView myView = new DataView(myDataSet.Tables["Orders"]);
		myView.RowFilter= "Convert(OrderAmount * 10,'System.Int32') % 2 = 0";
		myView.Sort =	"OrderDate DESC";
		myDataGrid.DataSource=myView;
	}
}
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform