eliasc

eliasc

  • NA
  • 1
  • 0

Hide Columns in DataGrid

Nov 7 2005 9:28 PM
Hi Everyone.

This is a simple problem which l simply can not work out whats wrong.

l am using C# and SQL CE . l can display a table in a datagrid with all fields however all l need to display is a few fields. Check out the following code.
----------------------------------------------------------------------------------------------

// create tables in Database

SqlCeConnection ssceconn = new SqlCeConnection(conString);
ssceconn.Open();
SqlCeCommand sqlCreateTable = ssceconn.CreateCommand();

sqlCreateTable.CommandText = "CREATE TABLE clientstable(client_id int IDENTITY(0,1) PRIMARY KEY, c_name nvarchar(50), c_address nvarchar(50), c_suburb nvarchar(50), c_state nvarchar(20), c_country nvarchar(30), c_abn nvarchar(30), c_contact1 nvarchar(50), c_contact2 nvarchar(50), c_telephone nvarchar(30), c_fax nvarchar(30), c_mobile nvarchar(30), c_terms nvarchar(20), c_category nvarchar(20), c_discount nvarchar(10), c_startdate datetime)";

sqlCreateTable.ExecuteNonQuery();

SqlCeCommand sqlInsertRow = ssceconn.CreateCommand();

sqlInsertRow.CommandText = "INSERT INTO clientstable(c_name, c_address, c_suburb, c_state, c_country, c_abn, c_contact1) VALUES ('ELIAS', '120 Down Town', 'Road', 'Melbourne', 'Victoria', 'Australia', '123 123 123')";

sqlInsertRow.ExecuteNonQuery();

//fill table

string sql = "SELECT * FROM clientstable";
try {

      DataSet sqlDS = new DataSet();
      System.Data.SqlServerCe.SqlCeDataAdapter sqlDA =
new SqlCeDataAdapter(sql, ssceconn);
      sqlDA.Fill(sqlDS);
      dataGrid1.DataSource = sqlDS.Tables[0].DefaultView;
     }

catch(Exception ex)
{}

// Setup dataGrid Style

DataGridTableStyle dgts = new DataGridTableStyle();

dgts.MappingName = "clientstable";
DataGridTextBoxColumn clientColumn =
new DataGridTextBoxColumn();
clientColumn.MappingName = "c_client";
clientColumn.HeaderText = "c_client";
clientColumn.Width = 0;
dgts.GridColumnStyles.Add(clientColumn);

DataGridTextBoxColumn nameColumn = new DataGridTextBoxColumn();

nameColumn.MappingName = "c_name";
nameColumn.HeaderText = "c_name";
nameColumn.Width = 50;
dgts.GridColumnStyles.Add(nameColumn);

DataGridTextBoxColumn addressColumn =
new DataGridTextBoxColumn();

addressColumn.MappingName = "c_address";
addressColumn.HeaderText = "c_address";
addressColumn.Width = 0;
dgts.GridColumnStyles.Add(addressColumn);


dataGrid1.TableStyles.Add(dgts);

ssceconn.Close();

}

catch(Exception ex){}

}
-------------------------------------------------------------------------------------------

The above code works interms of creating sql ce database and displaying in a dataGrid, however when trying to hide a column by setting its width=0 does not work.

Any ideas ?

Thanking you in advance !!!