I want my datagrid to only select rows, not cells.
Here is my code so far. I think I'm really close. This code still selects
just cell, but I want the row selected, and if the user arrows up or down
then the next row or previous row is selected. No mouse is used in this
application.
private void frmDealerSearch_Load(object sender,
System.EventArgs e)
{
string sConnString = "Data
Source=db;Database=License;Integrated Security=False;User
ID=sa;password=password";
string sProc = "prGet_DealerInfo";
using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand("Dealer_Info", oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;
oCmd.Parameters.Add("@sDealerNum",
SqlDbType.NChar, 6);
oCmd.Parameters["DEALER_NUMBER"].Value = "101043";
SqlDataAdapter oDa = new
SqlDataAdapter();
oDa.SelectCommand = oCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);
DataGridTableStyle tableStyle = new
DataGridTableStyle();
int numCols =
ds.Tables[0].Columns.Count;
DataGridNoActiveCellColumn
aColumnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
aColumnTextColumn = new
DataGridNoActiveCellColumn();
aColumnTextColumn.HeaderText
= ds.Tables[0].Columns[i].ColumnName;
aColumnTextColumn.MappingName = ds.Tables[0].Columns[i].ColumnName;
tableStyle.GridColumnStyles.Add(aColumnTextColumn) ;
}
dgDealerInfo.TableStyles.Clear();
dgDealerInfo.TableStyles.Add(tableStyle);
dgDealerInfo.DataSource =
ds.Tables[0];
}
}
}
}
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 previous selection is valid
if(SelectedRow > -1 && SelectedRow <
source.List.Count + 1)
this.DataGridTableStyle.DataGrid.UnSelect(Selected Row);
SelectedRow = rowNum;
this.DataGridTableStyle.DataGrid.Select(SelectedRo w);
}
}