473,748 Members | 7,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Da taSource 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) ;

DataGridTableSt yle dgts = new DataGridTableSt yle();
dgts.MappingNam e = "LicPrivilg es";

dgts.GridColumn Styles[-1].Width = 0;
dgPrivileges.Ta bleStyles.Add(d gts);

dt.Clear();

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

Nov 17 '05 #1
24 10759
Mike,

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

DataTable dt = dgPrivileges.Da taSource 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 (DataGridViewCo lumn column in
dgPrivileges.Co lumns)
{
column.Width = 0;
}

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

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

Marc
MCP.NET, MCAD.NET

<A href="http://www.statera.com ">Technolog y > 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.Da taSource 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) ;

DataGridTableSt yle dgts = new DataGridTableSt yle();
dgts.MappingNam e = "LicPrivilg es";

dgts.GridColumn Styles[-1].Width = 0;
dgPrivileges.Ta bleStyles.Add(d gts);

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 DataGridColumnS tyle
is an abstract class, in order to add a new DataGridColumnS tyle you
must either implement your own rendition of the DataGridColumnS tyle
abstract class or use one of the predefined DataGridColumnS tyles like
DataGridTextBox Column or DataGridBoolCol umn. Here is the code that
will get you the desired results:

DataTable dt = dgPrivileges.Da taSource 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 );
DataGridTableSt yle dgts = new DataGridTableSt yle();
dgts.MappingNam e = "LicPrivilg es";
/*************** *************** *************** *************** *************** *************** *************** ******
DataGridColumnS tyle colStyle = new
DataGridTextBox Column();

dgts.GridColumn Styles.Add(colS tyle);

dgts.GridColumn Styles[0].Width = 0;
dgPrivileges.Ta bleStyles.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.GridColumn Styles property? Because dgts
is a newly created empty DataGridTableSt yle, there are no column style in
dgts.GridColumn Styles 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.

DataGridColumnS tyle colStyle = new DataGridTextBox Column();
dgts.GridColumn Styles.Add(colS tyle);
dgts.GridColumn Styles[0].Width = 0;
dgts.GridColumn Styles[1].Width = 0;
dgts.GridColumn Styles[2].Width = 0;
dgts.GridColumn Styles[3].Width = 200;
dgts.GridColumn Styles[4].Width = 200;
dgts.GridColumn Styles[5].Width = 0;
dgts.GridColumn Styles[6].Width = 0;
dgts.GridColumn Styles[7].Width = 0;
dgPrivileges.Ta bleStyles.Add(d gts);

"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 DataGridColumnS tyle
is an abstract class, in order to add a new DataGridColumnS tyle you
must either implement your own rendition of the DataGridColumnS tyle
abstract class or use one of the predefined DataGridColumnS tyles like
DataGridTextBox Column or DataGridBoolCol umn. Here is the code that
will get you the desired results:

DataTable dt = dgPrivileges.Da taSource 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 );
DataGridTableSt yle dgts = new DataGridTableSt yle();
dgts.MappingNam e = "LicPrivilg es";
/*************** *************** *************** *************** *************** *************** *************** ******
DataGridColumnS tyle colStyle = new
DataGridTextBox Column();

dgts.GridColumn Styles.Add(colS tyle);

dgts.GridColumn Styles[0].Width = 0;
dgPrivileges.Ta bleStyles.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.Da taSource 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) ;

DataGridTableSt yle dgts = new DataGridTableSt yle();
dgts.MappingNam e = "LicPrivilg es";

DataGridColumnS tyle colStyle = new DataGridTextBox Column();
dgts.GridColumn Styles.Add(colS tyle);
dgts.GridColumn Styles[0].Width = 0;
dgts.GridColumn Styles[1].Width = 0;
dgts.GridColumn Styles[2].Width = 0;
dgts.GridColumn Styles[3].Width = 200;
dgts.GridColumn Styles[4].Width = 200;
dgts.GridColumn Styles[5].Width = 0;
dgts.GridColumn Styles[6].Width = 0;
dgts.GridColumn Styles[7].Width = 0;
dgPrivileges.Ta bleStyles.Add(d gts);

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.GridColumn Styles property? Because dgts
is a newly created empty DataGridTableSt yle, there are no column style in
dgts.GridColumn Styles 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
DataGridColumnS tyle, setting each DataGridColumnS tyle.MappingNam e 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 DataGridColumnS tyle 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.GridColumn Styles[3].Width = 200;

Here is my code so far.

DataTable dt = dgPrivileges.Da taSource 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) ;

DataGridTableSt yle dgts = new DataGridTableSt yle();
dgts.MappingNam e = "LicPrivilg es";

DataGridColumnS tyle colStyle = new DataGridTextBox Column();
dgts.GridColumn Styles.Add(colS tyle);
dgts.GridColumn Styles[3].Width = 200;

DataGridColumnS tyle colStyle2 = new DataGridTextBox Column();
dgts.GridColumn Styles.Add(colS tyle2);
dgts.GridColumn Styles[4].Width = 200;
dgPrivileges.Ta bleStyles.Add(d gts);

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
DataGridColumnS tyle, setting each DataGridColumnS tyle.MappingNam e 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 DataGridColumnS tyle 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.GridColumn Styles, you should use
dgts.GridColumn Styles[0] to access this columnstyle, instead of
dgts.GridColumn Styles[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.GridColumn Styles[4] to
dgts.GridColumn Styles[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.Da taSource 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) ;

DataGridTableSt yle dgts = new DataGridTableSt yle();
dgts.MappingNam e = "LicPrivilg es";

DataGridColumnS tyle colStyle = new DataGridTextBox Column();
dgts.GridColumn Styles.Add(colS tyle);
dgts.GridColumn Styles[0].Width = 200;

DataGridColumnS tyle colStyle2 = new DataGridTextBox Column();
dgts.GridColumn Styles.Add(colS tyle2);
dgts.GridColumn Styles[1].Width = 200;
dgPrivileges.Ta bleStyles.Add(d gts);

dt.Clear();

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

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

Thanks for your feedback.

After adding colStyle into dgts.GridColumn Styles, you should use
dgts.GridColumn Styles[0] to access this columnstyle, instead of
dgts.GridColumn Styles[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.GridColumn Styles[4] to
dgts.GridColumn Styles[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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3378
by: Prateek | last post by:
Hi, How can I change the width of a column in datagrid control? TIA Prateek
9
3945
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 selection column on the left. I have the widths of the columns. What other values do I need to include in the DataGrid width? Thanx,
3
4260
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 of a subclassed DataGridTextBoxColum dos not seem like a practical way to do it. I have subclassed a DataGrid and overridden the OnPaint as such:
2
6412
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
1828
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 with a buttonRow that does a delete function for me. Microsoft support helped me back then to get this done. Here is part of the code that creates the dataGrid: Dim ButtonColStyle As DataGridButtonColumn
17
7434
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 columns in it but i need to add a sixth column which will be my checkbox column - any help or pointers with this would be great
6
2749
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 freeze.
2
10404
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 actually happens is... when the Text size is too big... the column
2
1775
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 said the style name had to be the same as the table name. Perhaps the are refering to ts1.mappingname must be the same as the table name (datagrid1.datamember) because I've read that that is how the datagrid associates a style with what it's...
2
6593
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 in the child datagrid and then go to the combobox and choose another vendor. When the new vendor is loaded nothing shows in the datagrid but the itemsource shows the info is there. Know if I click on the child cell and then click back on the...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.