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;这种方法很有效但是把所有列都定义成一个长度,
各位,请问还有什么其它的方法
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;这种方法很有效但是把所有列都定义成一个长度,
各位,请问还有什么其它的方法
解决方案 »
- datagridview选中行数据添加到另外窗体的datagridview
- 【分享】发现一个有趣的事情。
- 在同一个form里,oledbDataAdapter的实例怎样可以重复使用?
- 笨人正在学做Controls,down了一个源代码。其中一段不明白。会做自定义控件的请进(windows)
- DataSet中对Table查询的问题
- c#.net 用mvc框架 用手写码实现分页功能
- .net版本问题。急!!!
- VS调用VB.NET函数连接SAP,无法创建ActiveX组件
- 一网站的结构是login画面->search画面->result画面,我的C#程序放在Client端,需要直接访问result画面,但要求必须login,如何处理呢?
- 散分拉!ghj1976老兄你快来up一下啊!
- 如何在TextBox编辑时,按下回车就运行一个按钮单击事件.
- 请教gridview动态绑定的问题?
资料一、使用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
我用另一种方法可以检测到长度是否大于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是设置长度,当它的长度大于设定的长度就不能输入一样.请大家一块参与讨论下,讨论都有分啊.