Mends.One

Add dummy datagrid columns to compact framework?

C#, Compact Framework, Datagrid, Windows Mobile

I'm creating a datagrid and binding it to a datasource, one of the columns needs to have data generated in a way that cannot be done with an sql query.

Is it possible to create a dummy column that's just blank and I can override the OnPaint function to make sure it draws the correct data I want? I tried adding a column without a mappingname but that just won't render.

0
M
meds
Jump to: Answer 1

Answers (1)

Like josef said up in the comments, it is simply a matter of adding a column.

This example would be better if the GetTableMethod were linked to your data layer, but it should get the point across:

private const string sqlConnString = "Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;" +
  "User ID=myDomain\myUsername;Password=myPassword;";
DataGrid dataGrid1;

private void BindData() {
  var table = GetTableMethod("SELECT * FROM Employees;");
  var cInt = table.Columns.Add("My Integer", typeof(int));
  var cStr = table.Columns.Add("Hex View", typeof(string));
  var cBool = table.Columns.Add("Is Even", typeof(bool));
  var dDbl = table.Columns.Add("My Double", typeof(double));
  for (int i = 0; i < table.Rows.Count; i++) { // can't use a foreach here
    DataRow row = table.Rows[i];
    row[cInt] = i;
    row[cStr] = i.ToString("X"); // view in hexadecimal, just for fun
    row[cBool] = ((i % 2) == 0);
    row[dDbl] = (double)i / table.Rows.Count;
  }
  dataGrid1.DataSource = table;
}

private DataTable GetTableMethod(string sqlSelectText) {
  var table = new DataTable();
  using (var cmd = new System.Data.SqlClient.SqlCommand(sqlSelectText, new System.Data.SqlClient.SqlConnection(sqlConnString))) {
    try {
      cmd.Connection.Open();
      table.Load(cmd.ExecuteReader());
    } finally {
      cmd.Connection.Close();
    }
  }
  return table;
}
2
U
user153923

Related Questions