473,386 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Datagrid Column Width

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
24 10723
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Prateek | last post by:
Hi, How can I change the width of a column in datagrid control? TIA Prateek
9
by: web1110 | last post by:
Hi y'all, I have resized the columns in a DataGrid and I want to set the width of the DataGrid to fit the columns. Just summing the column widths is too short due to the grid and gray row...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
4
by: Steve | last post by:
I am fairly new to VB.NET, and I am rewriting an application I wrote a while back, also in VB.NET. I aplied some new things I learned. Anyway, here is my problem....... I have a custom DataGrid...
17
by: Mike Fellows | last post by:
im trying (unsucessfully) to add a checkbox column to my datagrid i basically have a datagrid that im populating from a dataset Me.DataGrid1.DataSource = ds.Tables(0) the datagrid then has 5...
6
by: Agnes | last post by:
I understand it is impossible, but still curious to know "Can I freeze several column in the datagrid, the user can only scroll the first 3 columns (not verical), for the rest of the coulumn, it is...
2
by: Charleees | last post by:
Hi all, I have a DataGrid with Template Columns..... There are LAbels,Linkbuttons in the Single Row.. I have to set the Constant Column width for those Template Columns in Grid... Wat...
2
by: cj | last post by:
I was looking over some of my 2003 code today (see below) that loads a foxpro table via oledb connection. I used a sub "autosizecolumns" I found on the web but I never quite understood why they...
2
by: =?Utf-8?B?Y3JlYXZlczA2MjI=?= | last post by:
I have a nested datagrid in a xaml file, the parent datagrid loads the vendor information and the details loads the documents for that vendor in a datagrid. Everything is working fine until I click...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.