Help - How to disable TabStop in a DataGrid column

B

Bob Libby

I'm trying to skip the ItemDescription column when the user tabs off the
ItemNumber column in the following code. The DataGridTextBoxColumn.TabStop
property doesn't seem to have any effect. The code uses a DataGrid called
dataGrid1.



Any help would be appreciated.

Thanks,







public Form1()

{

InitializeComponent();

DataTable table = new DataTable("OrderDetail");

table.Columns.Add("ItemNumber", typeof(string));

table.Columns.Add("ItemDescription", typeof(string));

table.Columns.Add("Price", typeof(decimal));

DataRow dataRow = table.NewRow();

dataRow["ItemNumber"] = "ITEM1";

dataRow["ItemDescription"] = "ITEM1 DESC";

dataRow["Price"] = 5.0;

table.Rows.Add(dataRow);

dataRow = table.NewRow();

dataRow["ItemNumber"] = "ITEM2";

dataRow["ItemDescription"] = "ITEM2 DESC";

dataRow["Price"] = 6.0;

table.Rows.Add(dataRow);

DataGridTableStyle tableStyle = new DataGridTableStyle();

tableStyle.MappingName = "OrderDetail";


DataSet ds = new DataSet("OrderDetail");

DataGridTextBoxColumn column = new DataGridTextBoxColumn();

column.HeaderText = "Item Number";

column.MappingName = "ItemNumber";

tableStyle.GridColumnStyles.Add(column);

column = new DataGridTextBoxColumn();

column.HeaderText = "Description";

column.MappingName = "ItemDescription";

column.ReadOnly = true;

column.TextBox.TabStop = false;

tableStyle.GridColumnStyles.Add(column);

column = new DataGridTextBoxColumn();

column.HeaderText = "Price";

column.MappingName = "Price";

column.Format = "c";

column.Alignment = HorizontalAlignment.Right;

tableStyle.GridColumnStyles.Add(column);

dataGrid1.TableStyles.Add(tableStyle);

ds.Tables.Add(table);

dataGrid1.SetDataBinding(ds, table.TableName);

}
 
Y

Ying-Shen Yu[MSFT]

Hi Bob,

How about do it this way?
We can handle the Enter event of the inner TextBox and set the CurrentCell
to the next cell.
Here is a sample code snippet:
<code>
private void Form1_Load(object sender, System.EventArgs e)
{
oleDbDataAdapter1.Fill(dataSet11);
DataGridTextBoxColumn tbc =
dataGrid1.TableStyles["Categories"].GridColumnStyles["CategoryName"] as
DataGridTextBoxColumn;
tbc.TextBox.Enter +=new EventHandler(TextBox_Enter);
}

private void TextBox_Enter(object sender, EventArgs e)
{
//prevent entering the edit mode.
(sender as Control).Visible = false;
dataGrid1.CurrentCell = new
DataGridCell(dataGrid1.CurrentCell.RowNumber,dataGrid1.CurrentCell.ColumnNum
ber +1);
}
</code>

Does it solve your problem?



Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
B

Bob Libby

Thanks for your help, it worked well. I just added a small change to jump to
the next row if it's the last column.


private void textBox_ReadOnlyEnter(object sender, EventArgs e)

{

DataGridTextBox textBox = (DataGridTextBox)sender;

DataGrid dataGrid = (DataGrid)textBox.Parent;

textBox.Visible = false;

if (dataGrid1.CurrentCell.ColumnNumber+1 <
dataGrid.TableStyles[0].GridColumnStyles.Count)

{

dataGrid1.CurrentCell = new DataGridCell(dataGrid1.CurrentCell.RowNumber,
dataGrid1.CurrentCell.ColumnNumber+1);

}

else

{

dataGrid1.CurrentCell = new DataGridCell(dataGrid1.CurrentCell.RowNumber+1,
0);

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top