数据网格列宽 [英] Datagrid Column Width

查看:75
本文介绍了数据网格列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个Win表单,在C#2005中。

我想加载一个数据网格,使一些列宽0,然后清理
$ b我添加了$ b记录。


我收到错误消息," Index超出范围。必须是非负面的并且小于集合的大小
。参数名称:index""


我试过,-1,0,1,2索引,我收到相同的错误信息

每次尝试。

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(异常错误)

{

MessageBox.Show(err.Message);

}

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);
}

推荐答案

Mike,


而不是经历定义自定义的所有麻烦网格

样式,为什么不使用简单的循环:


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(

dgPrivileges.Columns中的DataGridViewColumn专栏)

{

column.Width = 0;

}


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


dt.Clear();

}

catch(异常错误)

{

MessageBox.Show(err.Message);

}


Marc

MCP.NET ,MCAD.NET


Mike L写道:
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:
这是一个Win形式,在C#2005中。

我想加载一个datagrid,使一些列宽0,然后清理我添加的
记录。

我收到错误消息," Index超出范围。必须是非负的且小于集合的大小。参数名称:index""

我试过,-1,0,1,2索引,每次尝试都会收到相同的错误信息。

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

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

dt.Rows.Add(dr);

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

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

dt.Clear();

}
catch(异常错误)
{MessageBox。 Show(err.Message);
}
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);
}






迈克,


我我猜这真的没有回答你的问题。真正的

问题是你没有将GridColumnStyle添加到

GridTableStyle。但是要注意的是DataGridColumnStyle

是一个抽象类,为了添加一个新的DataGridColumnStyle,你需要实现自己的DataGridColumnStyle再现

抽象类或使用预定义的DataGridColumnStyles之一,例如

DataGridTextBoxColumn或DataGridBoolColumn。以下是

将为您提供所需结果的代码:

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(异常错误)

{

MessageBox.Show(err.Message);

}

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 );
}


嗨Cadel,


感谢您的帖子。


我不确定我理解您的要求,您能否详细解释一下?为什么

你使用-1作为dgts.GridColumnStyles属性的参数?因为dgts

是一个新创建的空DataGridTableStyle,所以

dgts.GridColumnStyles集合中没有列样式,所以传递给这个属性的任何索引都将是

导致索引超出范围例外。


对于这个问题,我认为你应该首先向我们解释你想做什么,

然后我们可以为你提供正确的做法这个。谢谢


祝你好运,

Jeffrey Tan

微软在线合作伙伴支持

安全! - www.microsoft.com/security

此帖子按原样提供没有保证,也没有赋予任何权利。

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.


这篇关于数据网格列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆