Works with table but not SP.

G

Guest

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.ColumnName;


aColumnTextColumn.MappingName = ds.Tables[0].Columns.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(SelectedRow);
SelectedRow = rowNum;
this.DataGridTableStyle.DataGrid.Select(SelectedRow);
}
}
 
J

Jeffrey Tan[MSFT]

Hi Cadel,

Thanks for your post.

Based on my understanding, you want to select the entire row when user
click certain cell.

I think we can get this done with a much easier way: we can handle
DataGrid.MouseUp event, then do the hittest in that event and select the
entire row. For details, please refer to the link below:
"5.11 How can I select the entire row when the user clicks on a cell in the
row?"
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q689q

Hope this helps
=============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

As I stated before, " No mouse is used in this application." I was really
looking forward to a solution this morning, but now I'm angery that you gave
me a mouse click code, when I stated that no mouse is used in this
application.

This is a WinForm, and the mouse will not be used.

I've pasted my question in again, for you or anyone to provide me a solution
to set the datagrid to only select rows, not cells. I don't know how much
more to make that clear. But I'll try.

PROBLEM: when the datagrid first appears the focus is on the first cell,
(top left), if the user arrows down the cell below is then selected.

I WANT: the datagrid when it first appears to have the first row selected
AND WHEN THE USER CLICKS THE DOWN ARROW THE ROW BELOW IS SELECTED. If the
user then presses the up arrow the previous ROW is selected. When the user
presses the ENTER key the values from the selected row are stored in an array.

Here is the first post:

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.ColumnName;


aColumnTextColumn.MappingName = ds.Tables[0].Columns.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(SelectedRow);
SelectedRow = rowNum;
this.DataGridTableStyle.DataGrid.Select(SelectedRow);
}
}



"Jeffrey Tan[MSFT]" said:
Hi Cadel,

Thanks for your post.

Based on my understanding, you want to select the entire row when user
click certain cell.

I think we can get this done with a much easier way: we can handle
DataGrid.MouseUp event, then do the hittest in that event and select the
entire row. For details, please refer to the link below:
"5.11 How can I select the entire row when the user clicks on a cell in the
row?"
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q689q

Hope this helps
=============================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Cadel,

Sorry, it seems that I mislooked that "not using mouse" sentence.

If you do not want to use mouse, I think your code should meet your need. I
have tested your code snippet on my side. It works well: when the DataGrid
first appears, when DataGridNoActiveCellColumn.Edit method is called,
because we did not call the base version of Edit method, the first cell
will not become editable state, instead, our code will select the first
row. My test code listed below:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.TableName="Test";
dt.Columns.Add(new DataColumn("column1", typeof(string)));
dt.Columns.Add(new DataColumn("column2", typeof(int)));

for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]="item"+i.ToString();
dr["column2"]=i;
dt.Rows.Add(dr);
}

DataGridTableStyle dgts=new DataGridTableStyle();
dgts.MappingName="Test";

DataGridNoActiveCellColumn dgnacc=new DataGridNoActiveCellColumn();
dgnacc.MappingName="column1";
dgnacc.Width=100;
dgts.GridColumnStyles.Add(dgnacc);

dgnacc=new DataGridNoActiveCellColumn();
dgnacc.MappingName="column2";
dgts.GridColumnStyles.Add(dgnacc);

this.dataGrid1.TableStyles.Add(dgts);

this.dataGrid1.DataSource=dt;
}

public class DataGridNoActiveCellColumn : DataGridTextBoxColumn
{
private int SelectedRow = -1;
protected override void Edit(CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText,
bool cellIsVisible)
{
//base.Edit (source, rowNum, bounds, readOnly, instantText,
cellIsVisible);
//this.DataGridTableStyle.DataGrid.Select(rowNum);
//make sure previous selection is valid
if(SelectedRow > -1 && SelectedRow <
source.List.Count + 1)

this.DataGridTableStyle.DataGrid.UnSelect(SelectedRow);
SelectedRow = rowNum;
this.DataGridTableStyle.DataGrid.Select(SelectedRow);

}
}
Does this code snippet work on your side? Do you forget the add
DataGridNoActiveCellColumn into DataGrid's column style collection? Do you
set the column style MappingName property correctly?

If you still have any concern, please feel free to tell me, thanks
=======================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Now I know what the problem is. I'm using a SP not a table. Your code
worked for me too, because you created a table. But if your code accessed a
SP, then it wouldn't work, like how my code is not working now.

I also tested creating a table (like you did) in my code and it worked, but
I want to use a SP.

So, now the question is, how do I get my code to work using a SP?


"Jeffrey Tan[MSFT]" said:
Hi Cadel,

Sorry, it seems that I mislooked that "not using mouse" sentence.

If you do not want to use mouse, I think your code should meet your need. I
have tested your code snippet on my side. It works well: when the DataGrid
first appears, when DataGridNoActiveCellColumn.Edit method is called,
because we did not call the base version of Edit method, the first cell
will not become editable state, instead, our code will select the first
row. My test code listed below:

private void Form1_Load(object sender, System.EventArgs e)
{
DataTable dt=new DataTable();
dt.TableName="Test";
dt.Columns.Add(new DataColumn("column1", typeof(string)));
dt.Columns.Add(new DataColumn("column2", typeof(int)));

for(int i=0;i<5;i++)
{
DataRow dr=dt.NewRow();
dr["column1"]="item"+i.ToString();
dr["column2"]=i;
dt.Rows.Add(dr);
}

DataGridTableStyle dgts=new DataGridTableStyle();
dgts.MappingName="Test";

DataGridNoActiveCellColumn dgnacc=new DataGridNoActiveCellColumn();
dgnacc.MappingName="column1";
dgnacc.Width=100;
dgts.GridColumnStyles.Add(dgnacc);

dgnacc=new DataGridNoActiveCellColumn();
dgnacc.MappingName="column2";
dgts.GridColumnStyles.Add(dgnacc);

this.dataGrid1.TableStyles.Add(dgts);

this.dataGrid1.DataSource=dt;
}

public class DataGridNoActiveCellColumn : DataGridTextBoxColumn
{
private int SelectedRow = -1;
protected override void Edit(CurrencyManager source,
int rowNum, Rectangle bounds, bool readOnly, string instantText,
bool cellIsVisible)
{
//base.Edit (source, rowNum, bounds, readOnly, instantText,
cellIsVisible);
//this.DataGridTableStyle.DataGrid.Select(rowNum);
//make sure previous selection is valid
if(SelectedRow > -1 && SelectedRow <
source.List.Count + 1)

this.DataGridTableStyle.DataGrid.UnSelect(SelectedRow);
SelectedRow = rowNum;
this.DataGridTableStyle.DataGrid.Select(SelectedRow);

}
}
Does this code snippet work on your side? Do you forget the add
DataGridNoActiveCellColumn into DataGrid's column style collection? Do you
set the column style MappingName property correctly?

If you still have any concern, please feel free to tell me, thanks
=======================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Cadel,

Thanks for your feedback.

After doing some research I found the problem in your code. No, the problem
has nothing to do with SP. Because after filling the DataSet/DataTable, the
Winform databinding will has nothing to do with where the data is from. The
DataBinding only concerns with the datasource.

Actually, your code is almost correct, except that it forgets to set the
DataGridTableStyle.MappingName property, so when DataGrid is populating the
data, it can not successfully map DataTable to the DataGridTableStyle. So
it will use its default DataGridTableStyle and DataGridColumnStyle etc...
It just ignores our DataGridTableStyle.

After adding this line of code, your code should work well:

dgDealerInfo.TableStyles.Clear();
tableStyle.MappingName=ds.Tables[0].TableName; //this
correctly sets the MappingName property
dgDealerInfo.TableStyles.Add(tableStyle);
dgDealerInfo.DataSource = ds.Tables[0];

Hope this helps.
=============================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Cadel,

Does my reply resolve you problem? Do you still have any concern, please
feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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