By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,423 Members | 1,601 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,423 IT Pros & Developers. It's quick & easy.

Datagrid Column Width

P: n/a
This is for a Win form, in C# 2005.

I want to load a datagrid, make some columns width 0, and then clean out the
record I added.

I get the error message, ""Index was out of range. Must be non-negative and
less than the size of the collection. Parameter name: index""

I have tried, -1,0,1,2 for the index, and I get the same error message on
each try.
DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

dgts.GridColumnStyles[-1].Width = 0;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch(Exception err)
{
MessageBox.Show(err.Message);
}

Nov 17 '05 #1
Share this Question
Share on Google+
24 Replies


P: n/a
Mike,

Instead of going through all of the trouble of defining custom grid
styles, why not use a simple loop:

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();
dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;
dt.Rows.Add( dr );

//************************************************** **********
foreach (DataGridViewColumn column in
dgPrivileges.Columns)
{
column.Width = 0;
}

//************************************************** **********

dt.Clear();
}
catch (Exception err)
{
MessageBox.Show( err.Message );
}

Marc
MCP.NET, MCAD.NET

<A href="http://www.statera.com">Technology > Meet Business</A>
Mike L wrote:
This is for a Win form, in C# 2005.

I want to load a datagrid, make some columns width 0, and then clean out the
record I added.

I get the error message, ""Index was out of range. Must be non-negative and
less than the size of the collection. Parameter name: index""

I have tried, -1,0,1,2 for the index, and I get the same error message on
each try.
DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

dgts.GridColumnStyles[-1].Width = 0;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch(Exception err)
{
MessageBox.Show(err.Message);
}


Nov 17 '05 #2

P: n/a
Mike,

I guess that really doesn't answer your question though. The real
issue is that you did not add the GridColumnStyle to the
GridTableStyle. However, something to note is that DataGridColumnStyle
is an abstract class, in order to add a new DataGridColumnStyle you
must either implement your own rendition of the DataGridColumnStyle
abstract class or use one of the predefined DataGridColumnStyles like
DataGridTextBoxColumn or DataGridBoolColumn. Here is the code that
will get you the desired results:

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();
dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;
dt.Rows.Add( dr );
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "LicPrivilges";
/************************************************** ************************************************** ***********
DataGridColumnStyle colStyle = new
DataGridTextBoxColumn();

dgts.GridColumnStyles.Add(colStyle);

dgts.GridColumnStyles[0].Width = 0;
dgPrivileges.TableStyles.Add( dgts );
/************************************************** ************************************************** ***********
dt.Clear();
}
catch (Exception err)
{
MessageBox.Show( err.Message );
}

Nov 17 '05 #3

P: n/a
Hi Cadel,

Thanks for your post.

I am not sure I understand your request, can you explain it in details? Why
you use -1 as the parameter of dgts.GridColumnStyles property? Because dgts
is a newly created empty DataGridTableStyle, there are no column style in
dgts.GridColumnStyles collection, so any index passed to this property will
result in an "index out of range" exception.

For this issue, I think you should first explain to us what you want to do,
then we may provide you the correct way of doing this. 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.

Nov 17 '05 #4

P: n/a
Your code worked but when I added column 1, 2 and 3 it come up with the error
message again.

I want the user to only see column 5 and 6. I need the other columns for
calucations I run in the background.

DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 0;
dgts.GridColumnStyles[1].Width = 0;
dgts.GridColumnStyles[2].Width = 0;
dgts.GridColumnStyles[3].Width = 200;
dgts.GridColumnStyles[4].Width = 200;
dgts.GridColumnStyles[5].Width = 0;
dgts.GridColumnStyles[6].Width = 0;
dgts.GridColumnStyles[7].Width = 0;
dgPrivileges.TableStyles.Add(dgts);

"Marc" wrote:
Mike,

I guess that really doesn't answer your question though. The real
issue is that you did not add the GridColumnStyle to the
GridTableStyle. However, something to note is that DataGridColumnStyle
is an abstract class, in order to add a new DataGridColumnStyle you
must either implement your own rendition of the DataGridColumnStyle
abstract class or use one of the predefined DataGridColumnStyles like
DataGridTextBoxColumn or DataGridBoolColumn. Here is the code that
will get you the desired results:

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();
dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;
dt.Rows.Add( dr );
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = "LicPrivilges";
/************************************************** ************************************************** ***********
DataGridColumnStyle colStyle = new
DataGridTextBoxColumn();

dgts.GridColumnStyles.Add(colStyle);

dgts.GridColumnStyles[0].Width = 0;
dgPrivileges.TableStyles.Add( dgts );
/************************************************** ************************************************** ***********
dt.Clear();
}
catch (Exception err)
{
MessageBox.Show( err.Message );
}

Nov 17 '05 #5

P: n/a
I want the user to only see column 5 and 6. I need the other columns for
calucations I run in the background.

Here is the newest version of the code.

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 0;
dgts.GridColumnStyles[1].Width = 0;
dgts.GridColumnStyles[2].Width = 0;
dgts.GridColumnStyles[3].Width = 200;
dgts.GridColumnStyles[4].Width = 200;
dgts.GridColumnStyles[5].Width = 0;
dgts.GridColumnStyles[6].Width = 0;
dgts.GridColumnStyles[7].Width = 0;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch(Exception err)
{
MessageBox.Show(err.Message);
}

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

Thanks for your post.

I am not sure I understand your request, can you explain it in details? Why
you use -1 as the parameter of dgts.GridColumnStyles property? Because dgts
is a newly created empty DataGridTableStyle, there are no column style in
dgts.GridColumnStyles collection, so any index passed to this property will
result in an "index out of range" exception.

For this issue, I think you should first explain to us what you want to do,
then we may provide you the correct way of doing this. 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.

Nov 17 '05 #6

P: n/a
Hi Cadel,

Thanks for your feedback.

Based on my understanding, you only want to show 5th and 6th column in the
DataTable in DataGrid, yes? I think we could only create 2
DataGridColumnStyle, setting each DataGridColumnStyle.MappingName to 5th
and 6th DataColumn's column name. Then, only 5th and 6th DataColumn data
will show in DataGrid. While we still can use DataTable to use other
DataColumn's data. (Because we do not want to show these columns, there is
no need for us to create DataGridColumnStyle for these datacolumns)

Does this meet your need? 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.

Nov 17 '05 #7

P: n/a
Yes, I think it will meet my need, but I'm getting an error message, "Index
was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index." on dgts.GridColumnStyles[3].Width = 200;

Here is my code so far.

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[3].Width = 200;

DataGridColumnStyle colStyle2 = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[4].Width = 200;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch (Exception err)
{
MessageBox.Show(err.Message);
}

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

Thanks for your feedback.

Based on my understanding, you only want to show 5th and 6th column in the
DataTable in DataGrid, yes? I think we could only create 2
DataGridColumnStyle, setting each DataGridColumnStyle.MappingName to 5th
and 6th DataColumn's column name. Then, only 5th and 6th DataColumn data
will show in DataGrid. While we still can use DataTable to use other
DataColumn's data. (Because we do not want to show these columns, there is
no need for us to create DataGridColumnStyle for these datacolumns)

Does this meet your need? 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.

Nov 17 '05 #8

P: n/a
Hi Cadel,

Thanks for your feedback.

After adding colStyle into dgts.GridColumnStyles, you should use
dgts.GridColumnStyles[0] to access this columnstyle, instead of
dgts.GridColumnStyles[3]. This is because we only add 2 columns into the
TableStyle, and our index 0 Column style just mapping to the 4th DataColumn
in the DataTable. A more safe way is directly use dgts.Width to set this
column style width.
Note: we should also modify dgts.GridColumnStyles[4] to
dgts.GridColumnStyles[1] for the same reason.

Hope this helps

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.

Nov 17 '05 #9

P: n/a
Changed my code, no errors but I still see all the columns and the width
didn't change for the two columns.

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 200;

DataGridColumnStyle colStyle2 = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[1].Width = 200;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch (Exception err)
{
MessageBox.Show(err.Message);
}

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

Thanks for your feedback.

After adding colStyle into dgts.GridColumnStyles, you should use
dgts.GridColumnStyles[0] to access this columnstyle, instead of
dgts.GridColumnStyles[3]. This is because we only add 2 columns into the
TableStyle, and our index 0 Column style just mapping to the 4th DataColumn
in the DataTable. A more safe way is directly use dgts.Width to set this
column style width.
Note: we should also modify dgts.GridColumnStyles[4] to
dgts.GridColumnStyles[1] for the same reason.

Hope this helps

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.

Nov 17 '05 #10

P: n/a
Hi Cadel,

It's seems that you did not set the DataGridColumnStyle.MappingName to
corresponding DataColumn.ColumnName, if you set correctly, it should work.

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.

Nov 17 '05 #11

P: n/a
I thought, I did, "dgts.GridColumnStyles.Add(colStyle);"

What do you mean, "set the DataGridColumnStyle.MappingName to corresponding
DataColumn.ColumnName"?

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

It's seems that you did not set the DataGridColumnStyle.MappingName to
corresponding DataColumn.ColumnName, if you set correctly, it should work.

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.

Nov 17 '05 #12

P: n/a
Hi Cadel,

Thanks for your feedback.

"dgts.GridColumnStyles.Add(colStyle);" only add the new created 2 column
style2 into the DataGrid, however, without setting this
colStyle.MappingName property, DataGrid does not know this colStyle mapping
to which column in the DataTable. So we should add another line like below:
colStyle.MappingName=dt.Columns[index].ColumnName;

Change the "index" to the 4 or 5 or any column index you want to mapping in
the DataTable.

Hope this helps

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.

Nov 17 '05 #13

P: n/a
Thank you for your patience.

I am now getting an error message, "Index was out of range. Must be
non-negative and less than the size of the collection. Parameter name:
index", on line dgts.GridColumnStyles[3].Width = 200;

Here is my code.

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[3].Width = 200;
colStyle.MappingName = dt.Columns[4].ColumnName;

DataGridColumnStyle colStyle2 = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[4].Width = 200;
colStyle2.MappingName = dt.Columns[5].ColumnName;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch (Exception err)
{
MessageBox.Show(err.Message);
}

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

Thanks for your feedback.

"dgts.GridColumnStyles.Add(colStyle);" only add the new created 2 column
style2 into the DataGrid, however, without setting this
colStyle.MappingName property, DataGrid does not know this colStyle mapping
to which column in the DataTable. So we should add another line like below:
colStyle.MappingName=dt.Columns[index].ColumnName;

Change the "index" to the 4 or 5 or any column index you want to mapping in
the DataTable.

Hope this helps

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.

Nov 17 '05 #14

P: n/a
Hi Cadel,

Thanks for your feedback.

Oh, you get back to the original error in your code. You only added 2
DataGridTextBoxColumns into your dgts.GridColumnStyles, that is colStyle
and colStyle2. So you should use index 0 or 1 to refer these 2 column
styles, or you can directly use colStyle or colStyle2 references to
manipulate these 2 column style width.

Ok, I see that you must have some misunderstanding of the databinding of
Winform DataGrid. In our scenario, you only want to display 2 columns in UI
side, so we only created 2 DataGridTextBoxColumns, then add them into
DataGridTableStyle. So in DataGridTableStyle.GridColumnStyles property
collection, we can only use index 0 or 1(because there are only 2 column
styles, why do you use 3 and 4 as index?). However, these index 0 and 1
column style maps to index 4 and 5 DataColumn in the DataTable, so the UI
side and the DataTable side have different index to use. The modified code
listed below:

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 200; //or directly use
colStyle.Width=200;
colStyle.MappingName = dt.Columns[4].ColumnName;

DataGridColumnStyle colStyle2 = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[1].Width = 200;//or directly use
colStyle2.Width=200;
colStyle2.MappingName = dt.Columns[5].ColumnName;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch (Exception err)
{
MessageBox.Show(err.Message);
}

I hope this will be much clear now. 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.

Nov 17 '05 #15

P: n/a
No errors, but still the grid does not change, it still shows all the columns
at the default width.

This first block of code sets the data columns, the next block of code
enters values in the datagrid and changes the width of the columns so only 2
columns are seen on the user interface, and then clear the data out of the
grid. I add data and then clear the grid because the intial adding of data
to the grid takes a long time, so I add the intial data on load of the form,
use the user when entering data is not slowed down. The second block of code
does not seem to change the appearance of the datagrid.

private void txtLicYear_TextChanged(object sender, System.EventArgs e)
{
string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"];
string sProc = "prGet_LicenseCode";
using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

oCmd.Parameters.Add("@sLicenseYear", SqlDbType.NChar, 6);
oCmd.Parameters["@sLicenseYear"].Value = txtLicYear.Text;

SqlDataAdapter oDa = new SqlDataAdapter();

oDa.SelectCommand = oCmd;
DataSet ds=new DataSet();
oDa.Fill(ds);

int numTables = ds.Tables.Count;
//No table no records.
if (numTables < 1)
{
MessageBox.Show("No License Codes found for that year.", "No record
found", MessageBoxButtons.OK);
}
else
{
cboPrivilege.DataSource = ds.Tables[0];
cboPrivilege.DisplayMember = "LICENSE_CODE";
cboPrivilege.ValueMember = "LICENSE_CODE";
//cboPrivilege.SelectedIndex = -1;

DataTable dtGrid = ds.Tables[0].Clone();
dgPrivileges.DataSource = dtGrid;

}
}
}

}

private void frmDataEntry_Load(object sender, System.EventArgs e)
{
string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"];
MessageBox.Show(sConnString);
string sProc = "prGet_DefaultYear";
using (SqlConnection oCn = new SqlConnection(sConnString))
{
using (SqlCommand oCmd = new SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType = CommandType.StoredProcedure;

oCmd.Parameters.Add("@sDefaultYear", SqlDbType.NVarChar,
4);
oCmd.Parameters["@sDefaultYear"].Direction =
ParameterDirection.Output;

oCmd.ExecuteNonQuery();
oCn.Close();

txtLicYear.Text =
oCmd.Parameters["@sDefaultYear"].Value.ToString();
txtDateSold3.Text = txtLicYear.Text.Substring(2, 2);
txtTrip3.Text = txtLicYear.Text.Substring(2, 2);
cboAMPM.Text = "PM";
}
}
DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 200; //or directly use
colStyle.Width = 200;
colStyle.MappingName = dt.Columns[4].ColumnName;

DataGridColumnStyle colStyle2 = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[1].Width = 200; //or directly use
colStyle2.Width = 200;
colStyle2.MappingName = dt.Columns[5].ColumnName;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}

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

Thanks for your feedback.

Oh, you get back to the original error in your code. You only added 2
DataGridTextBoxColumns into your dgts.GridColumnStyles, that is colStyle
and colStyle2. So you should use index 0 or 1 to refer these 2 column
styles, or you can directly use colStyle or colStyle2 references to
manipulate these 2 column style width.

Ok, I see that you must have some misunderstanding of the databinding of
Winform DataGrid. In our scenario, you only want to display 2 columns in UI
side, so we only created 2 DataGridTextBoxColumns, then add them into
DataGridTableStyle. So in DataGridTableStyle.GridColumnStyles property
collection, we can only use index 0 or 1(because there are only 2 column
styles, why do you use 3 and 4 as index?). However, these index 0 and 1
column style maps to index 4 and 5 DataColumn in the DataTable, so the UI
side and the DataTable side have different index to use. The modified code
listed below:

DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

DataGridColumnStyle colStyle = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 200; //or directly use
colStyle.Width=200;
colStyle.MappingName = dt.Columns[4].ColumnName;

DataGridColumnStyle colStyle2 = new DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[1].Width = 200;//or directly use
colStyle2.Width=200;
colStyle2.MappingName = dt.Columns[5].ColumnName;
dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch (Exception err)
{
MessageBox.Show(err.Message);
}

I hope this will be much clear now. 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.

Nov 17 '05 #16

P: n/a
Hi Cadel,

Thanks for your feedback.

Sorry, but currently, I still can not reproduce out your issue, so it is
hard for us to give definit answer. Can you get rid of the database
dependency and provide a full code sample or project to help me reproduce
this issue? Then I can track this issue more accurate.

Addtionally, I am not sure why you invokes dt.Clear() after the setting the
tablestyle.

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.

Nov 21 '05 #17

P: n/a
As you can tell by my code and the large amount of posts on this thread, I am
very confused. :-)

To make it easier on us, I've decided to move all the data grid code to one
location in my project.

The reason I invoke dt.Clear(), as I stated in post 11/17/05,"
I add data and then clear the grid because the intial adding of data
to the grid takes a long time, so I add the intial data on load of the form,
the user when entering data is not slowed down."
I want to populate a combo box, and setup a data grid. The combo box will
show the data that the user can select. The data grid stores the choices
that the user makes from the combo box.

Here is my attempt at populating the combo box and setting up the data grid.

string sProc = "prGet_LicenseCode";
using (SqlConnection oCn = new
SqlConnection(sConnString))
{
using (SqlCommand oCmd = new
SqlCommand(sProc, oCn))
{
oCn.Open();
oCmd.CommandType =
CommandType.StoredProcedure;

oCmd.Parameters.Add("@sLicenseYear",
SqlDbType.NChar, 6);

oCmd.Parameters["@sLicenseYear"].Value = txtLicYear.Text;

SqlDataAdapter oDa = new
SqlDataAdapter();

oDa.SelectCommand = oCmd;
DataSet ds=new DataSet();
DataSet ds2 = new DataSet();
oDa.Fill(ds);
oDa.Fill(ds2);

int numTables = ds.Tables.Count;
//No table no records.
if (numTables < 1)
{
MessageBox.Show("No License
Codes found for that year.", "No record found", MessageBoxButtons.OK);

}
else
{
cboPrivilege.DataSource =
ds.Tables[0];
cboPrivilege.DisplayMember =
"LICENSE_CODE";
cboPrivilege.ValueMember =
"LICENSE_CODE";

//DataTable dtGrid = ds.Tables[0].Clone();
DataTable dtGrid = ds2.Tables[0];
dgPrivileges.DataSource = dtGrid;
DataTable dt = dgPrivileges.DataSource as DataTable;
try
{
DataRow dr = dt.NewRow();

dr[0] = 0;
dr[1] = 0;
dr[2] = 0;
dr[3] = 0;
dr[4] = 0;
dr[5] = 0;
dr[6] = 0;

dt.Rows.Add(dr);

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

DataGridColumnStyle colStyle = new
DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle);
dgts.GridColumnStyles[0].Width = 200; //or
directly use colStyle.Width = 200;
colStyle.MappingName = dt.Columns[4].ColumnName;

DataGridColumnStyle colStyle2 = new
DataGridTextBoxColumn();
dgts.GridColumnStyles.Add(colStyle2);
dgts.GridColumnStyles[1].Width = 200; //or
directly use colStyle2.Width = 200;
colStyle2.MappingName = dt.Columns[5].ColumnName;

dgPrivileges.TableStyles.Add(dgts);

dt.Clear();

}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}
}
Do I have to populate the datagrid with data, to setup columns?

How would you code, to populate a combo box and setup a datagrid to receive
data that will be the same as the combo box?
""Jeffrey Tan[MSFT]"" wrote:
Hi Cadel,

Thanks for your feedback.

Sorry, but currently, I still can not reproduce out your issue, so it is
hard for us to give definit answer. Can you get rid of the database
dependency and provide a full code sample or project to help me reproduce
this issue? Then I can track this issue more accurate.

Addtionally, I am not sure why you invokes dt.Clear() after the setting the
tablestyle.

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.

Nov 23 '05 #18

P: n/a
Hi Cadel,

Currently, it is hard for me to provide some useful information to track
out this problem. It is possible for you to narrow down this issue into a
reproduce project, then I will do some debugging in this reproduce project

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.

Nov 29 '05 #19

P: n/a
I have reproduced the project. How do I get it to you? Email address?

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

Currently, it is hard for me to provide some useful information to track
out this problem. It is possible for you to narrow down this issue into a
reproduce project, then I will do some debugging in this reproduce project

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.

Nov 30 '05 #20

P: n/a
Hi Cadel,

If you can, I suggest you attach it in the further reply, this can benifit
the entire community. However, if you failed to attach in the newsgroup,
you can contact me at v-*****@online.microsoft.com(remove online.). 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.

Dec 1 '05 #21

P: n/a
Did you get my email I sent on 12/1/05?

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

If you can, I suggest you attach it in the further reply, this can benifit
the entire community. However, if you failed to attach in the newsgroup,
you can contact me at v-*****@online.microsoft.com(remove online.). 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.

Dec 5 '05 #22

P: n/a
Hi Cadel,

Thanks for your feedback.

I am not aware "Liddiard, Michael [mc********@odwc.state.ok.us]" is you.

Sorry, the access mdb file has been filtered by our internal firewall.
However, I have received your Winform project. I do not know why you must
include the database file in the project. Can you get rid of the database
dependency and just create the datasource manually, which I think is not a
hard task.

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.

Dec 6 '05 #23

P: n/a
Hi Jeffrey,

I emailed you the modified code to "Jeffrey Tan [v-*****@microsoft.com]"

Let me know if you got it.

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

Thanks for your feedback.

I am not aware "Liddiard, Michael [mc********@odwc.state.ok.us]" is you.

Sorry, the access mdb file has been filtered by our internal firewall.
However, I have received your Winform project. I do not know why you must
include the database file in the project. Can you get rid of the database
dependency and just create the datasource manually, which I think is not a
hard task.

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.

Dec 19 '05 #24

P: n/a
Hi Cadel,

Thanks for your feedback.

Yes, I have received your project. Your current problem in the project is
the tablestyle setting does not work, yes?

After reviewing your project, I found that this is caused by the in-correct
setting on the DataGridTableStyle.MappingName property. In your Form.Load
code, you set this property like this:

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

However, we should set this property to DataTable.TableName. In your
scenario, DataTable.TableName is an empty string.(you can verify this by
debugger or using a statement to print out the result). After modifying
this to DataTable.TableName, it works fine now:
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName = dt.TableName;

Hope this helps

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.

Dec 20 '05 #25

This discussion thread is closed

Replies have been disabled for this discussion.