make win datagrid row selection / non-ediateble column

Posted: April 7, 2005 in Uncategorized

private

void Form1_Load(object sender, System.EventArgs e)

{

string connString = @"Integrated Security=SSPI;Packet Size=4096;Data Source=shreeman;Tag with column collation when possible=False;Initial Catalog=test;Use Procedure for Prepare=1;Auto Translate=True;Persist Security Info=False;Provider=SQLOLEDB.1;Workstation ID=SHREEMAN;Use Encryption for Data=False";

string sqlString = "SELECT * FROM emp";

OleDbDataAdapter dataAdapter =

null;

DataSet _dataSet =

null;

try

{

// Connection object

OleDbConnection connection =

new OleDbConnection(connString);

// Create data adapter object

dataAdapter =

new OleDbDataAdapter(sqlString, connection);

// Create a dataset object and fill with data using data adapter’s Fill method

_dataSet =

new DataSet();

dataAdapter.Fill(_dataSet, "emp");

connection.Close();

}

catch(Exception ex)

{

MessageBox.Show("Problem with DB access-\n\n connection: "

+ connString + "\r\n\r\n query: " + sqlString

+ "\r\n\r\n\r\n" + ex.ToString());

this.Close();

return;

}

// 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 = "emp";

// 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["emp"].Columns.Count;

DataGridNoActiveCellColumn aColumnTextColumn ;

for(int i = 0; i < numCols; ++i)

{

aColumnTextColumn =

new DataGridNoActiveCellColumn();

aColumnTextColumn.HeaderText = _dataSet.Tables["emp"].Columns[i].ColumnName;

aColumnTextColumn.MappingName = _dataSet.Tables["emp"].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);

dataGrid1.DataSource = _dataSet.Tables["emp"];

}

}

public class DataGridNoActiveCellColumn : DataGridTextBoxColumn

{

private int SelectedRow = -1;

protected override void Edit(System.Windows.Forms.CurrencyManager

source,

int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string

instantText,

bool cellIsVisible)

{

//make sure the selectrow is valid before trying to unselect

if(SelectedRow > -1 && SelectedRow < source.List.Count + 1)

this.DataGridTableStyle.DataGrid.UnSelect(SelectedRow);

SelectedRow = rowNum;

this.DataGridTableStyle.DataGrid.Select(SelectedRow);

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s