DataGridTableStyle and DataGrid - Hide DataGrid Column
Tech-Today

DataGridTableStyle and DataGrid - Hide DataGrid Column


I was developing another windows mobile which has the requirement of using a datagrid for dataview. A DataGrid is a good control in presenting your data [http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.aspx].

It has many customizable properties which you can see to fit your need, for example you can change the Columns bound in the datagrid. There are TextBox, Boolean, etc DataGrid column.

Unfortunately for windows mobile [CF1.1] the only available column type is DataGridTextBoxColumn [http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridtextboxcolumn.aspx].

Nevertheless I want, to customize the look: set the width, change the column header, etc. Let's try these 2 basic customization.

First we should setup the DataGridTableStyle, please follow the code (In this example we are customizing 3 columns, the first 1 is the ID):

/**
col1, col2 - datagrid columns,
- name should much the data row you have queried
head1, head2 - datagrid column header text
*/
private void UpdateGridStyle(DataGrid grid, string col1,
string col2, string head1,
string head2) {
//first we will clear the tablestyles,
//if not it will throw argument exception
//if there is already a style prior to this call
grid.TableStyles.Clear();
DataGridTableStyle tableStyle = new DataGridTableStyle();
//take note of this mapping name, it's very important
tableStyle.MappingName = "czetsuya";

//our first column which is the ID
DataGridTextBoxColumn gridColumn1 = new DataGridTextBoxColumn();
gridColumn1.Width = 0;
gridColumn1.MappingName = "ID";
gridColumn1.HeaderText = "ID";
tableStyle.GridColumnStyles.Add(gridColumn1);

//second column
DataGridTextBoxColumn gridColumn2 = new DataGridTextBoxColumn();
gridColumn2.Width = 100;
gridColumn2.MappingName = col1;
gridColumn2.HeaderText = head1;
tableStyle.GridColumnStyles.Add(gridColumn2);

//third column
DataGridTextBoxColumn gridColumn3 = new DataGridTextBoxColumn();
gridColumn3.Width = 100;
gridColumn3.MappingName = col2;
gridColumn3.HeaderText = head2;
tableStyle.GridColumnStyles.Add(gridColumn3);

grid.TableStyles.Add(tableStyle);
}

public DataTable QueryAsDataTable(string sql) {
DataTable dt = new DataTable();
try
{
conn.Open();
SqlCeDataAdapter da = new SqlCeDataAdapter(sql, conn);
da.Fill(dt);
}
catch(SqlCeException e)
{
//catch exception
}
finally
{
conn.Close();
}
//TAKE NOTE OF THIS MAPPING, without this the code will not work
//this mapping will be reference by the DataGridTableStyle
dt.TableName = "czetsuya";
return dt;
}
//we will call the above functions like this:

sql = "SELECT field1 AS ID, field2 AS col1, field3 AS col2
FROM table";
UpdateGridStyle("col1", "col2", "COLUMN1", "COLUMN1");
grid.DataSource = da.QueryAsDataTable(sql);




- How To Connect Msaccess Database From Java Using Jdbc Driver
The method is almost similar to my previous code, and like that it's better to explain it by code: private static Connection getConnection() { String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; String url = "jdbc:odbc:anime"; //anime is the database...

- How To Connect Mysql Or Postgresql Server From Java Using Jdbc Driver
How to connect MySql or PostgreSql Server from Java using JDBC Driver I think the best answer to this is through coding: private static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); //conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/anime",...

- Select Top, Limit Workaround On Sqlce 2.0 Or Limiting The Number Of Rows/getting A Range From A Dataset
Problem: -In SQLCE 2.0 it is documented that 2 of the common used keywords are not supported in gettings a range of records from a dataset. They are LIMIT and TOP which are use this way: LIMIT: SELECT * FROM tableA LIMIT 5, 10; This query will select...

- Where Is The Excel Autofit Column Width In Microsoft Office 2007
For those of you who want to show the whole column width of a cell just like the previous office, here's how (it took me a while): 1.) Open your excel document 2.) Make sure Home tab is selected (top left) 3.) Select all the cells, or the column you...

- Select All Checkbox Or Element In A Form Html
Creating a form dynamically at run time – this also means we should give unique names to each html controls. For example we have a datagrid like object and its first column is checkbox, how should we check all of the rows. Here is my code that iterates...



Tech-Today








.