winform程序中datagrid控件中各列长度不同长度限制如何设置???

hbqhdlc 2007-04-25 11:52:11
winform程序中datagrid控件中各列长度不同长度限制如何设置???
datagrid显示的数据库表内含列line_name,switch,password几列
我想设置line_name的长度不大于10,switch和password列的长度不大于4,该如何设置,我试过以下几种方法:
1,在TableStyles-GridColumnStyles-width(MappingName:line_name,switch,password),把上边几列.width属性分别设置10,4,4后列仍然可以存储更大长度数据;
2,int len=dg1[dg1.CurrentCell].ToString().Length;这种方法很有效但是把所有列都定义成一个长度,
各位,请问还有什么其它的方法
...全文
258 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbqhdlc 2007-04-26
  • 打赏
  • 举报
回复
非常感谢楼上的2位的帮助解答,这两种方法我都试过了,还是不行,
我用另一种方法可以检测到长度是否大于10或4,用数据集方式(dsline1:数据集)
for(int i=0;i<dsline1.Tables[0].Rows.Count;i++)
{
if(dsline1.Tables[0].Rows[i]["line_name"].ToString().Length>10)
{
MessageBox.Show("line_name长度不能大于10,请重新输入");
}
}
但这种方法只能保证长度超过10会提示,我想的是在datagrid显示的数据库表中,如果修改的话,长度超过10就不再显示,它只能显示10,就像textbox是设置长度,当它的长度大于设定的长度就不能输入一样.请大家一块参与讨论下,讨论都有分啊.
kong1122 2007-04-26
  • 打赏
  • 举报
回复
参考资料:希望能够对你有所帮助!
资料一、使用DataGridColumnStyle.Width属性,实际上在DataGrid中每个Column是对应于一个DataGridColumnStyle的。
资料二、 DataGrid1.PreferredColumnWidth = 100
资料三、用DataGridTableSytle来做。
下面是MSDN中的示例代码

Private Sub AddCustomDataTableStyle()
' Create a new DataGridTableStyle and set
' its MappingName to the TableName of a DataTable.
Dim ts1 As New DataGridTableStyle()
ts1.MappingName = "Customers"

' Add a GridColumnStyle and set its MappingName
' to the name of a DataColumn in the DataTable.
' Set the HeaderText and Width properties.

Dim boolCol As New DataGridBoolColumn()
boolCol.MappingName = "Current"
boolCol.HeaderText = "IsCurrent Customer"
boolCol.Width = 150
ts1.GridColumnStyles.Add(boolCol)

' Add a second column style.
Dim TextCol As New DataGridTextBoxColumn()
TextCol.MappingName = "custName"
TextCol.HeaderText = "Customer Name"
TextCol.Width = 250
ts1.GridColumnStyles.Add(TextCol)

' Create the second table style with columns.
Dim ts2 As New DataGridTableStyle()
ts2.MappingName = "Orders"

' Change the colors.
ts2.ForeColor = Color.Yellow
ts2.AlternatingBackColor = Color.Blue
ts2.BackColor = Color.Blue

' Create new DataGridColumnStyle objects.
Dim cOrderDate As New DataGridTextBoxColumn()
cOrderDate.MappingName = "OrderDate"
cOrderDate.HeaderText = "Order Date"
cOrderDate.Width = 100
ts2.GridColumnStyles.Add(cOrderDate)

Dim pcol As PropertyDescriptorCollection = Me.BindingContext(myDataSet, "Customers.custToOrders").GetItemProperties()

Dim csOrderAmount As New DataGridTextBoxColumn(pcol("OrderAmount"), "c", True)
csOrderAmount.MappingName = "OrderAmount"
csOrderAmount.HeaderText = "Total"
csOrderAmount.Width = 100
ts2.GridColumnStyles.Add(csOrderAmount)

' Add the DataGridTableStyle objects to the collection.
myDataGrid.TableStyles.Add(ts1)
myDataGrid.TableStyles.Add(ts2)
End Sub 'AddCustomDataTableStyle
zpingy 2007-04-26
  • 打赏
  • 举报
回复
就应该是设置width吧,还有别的属性吗?
netusemaster 2007-04-26
  • 打赏
  • 举报
回复
DG属性ITEMS 里面 设置 WEITH
hbqhdlc 2007-04-26
  • 打赏
  • 举报
回复
大家帮忙看一下啊,急用啊.

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧