关于WinForm下DataGridView控件行的选择的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于WinForm下DataGridView控件行的选择的问题相关的知识,希望对你有一定的参考价值。
我在DataGridView1控件中显示考题的信息,双击某一行考题后可以将这行考题添加到DataGridView2,考题有分类QuestionsType字段,我想在选择了QuestionsType的值是A的考题后,设置QuestionsType的值为B的考题都为不可选考题,有什么办法可以实现这个操作吗?我的意思是在DataGridView1中我显示了所有可选考题的信息,双击某一行后,可以把这一行的考题信息添加到DataGridView2中,那么这个时候,我就要判断一下我先择考题的QuestionsType值,如果是A的话,那么我就要让DataGridView1中所有QuestionsType值为B的考题是不可选的。请各位大侠指点一下,谢谢了。
在1里面绑定一个id列,隐藏,然后你通过datagridview的行选择事件,取得选中行的id,再通过id去数据库查QuestionsType,然后根据条件查出数据给2绑定数据,同时设置行的datagridview[列,行].readonly=true; 参考技术A 你最好截个图,尤其是数据库,没有明白你的意思。尤其是那个字段要干嘛。一个Winform下DataGridView控件外观的自定义类
最近非常频繁的使用DataGridView控件,VS提供的Winform下DataGridView的默认样式非常难看。如果做美化处理的话,需要调整的地方非常多,且该控件的很多设置属性非常的晦涩,不是很人性化。每次进行设置都煞费脑筋,并且需要设置的DataGridView控件又比较多,如果手工一个一个调整非常麻烦。如果在每个窗体里逐个一行一行的用代码来设置,会造成代码很凌乱、琐碎,因此我对DataGridView格式设置的常用项封装到一个Helper类,通过这个帮助类可以一步到位完成设置。
对DataGridView控件,我需要完成的一些基本的格式控制,包括以下一些内容:
1、标题行背景色、前景色、字体设置、平面化样式、行高
2、标题列宽度,平面化样式
3、偶数行背景色、前景色
4、奇数行背景色、前景色
5、数据行高度、字体
6、列宽度,对齐方式
7、禁止编辑、添加、删除、调整行高、列宽、排序
8、去除控件外边框
最终处理过后的风格类似如下图所示
最终我所希望的实现格式化DataGridView控件的形式是通过一行代码来实现,类似如下所示:
1 Dim mGridHelper As New CGridHelper(GridVersion, Color.FromArgb(250, 250, 250), 2 New Font("微软雅黑", 10), 30, Color.FromArgb(159, 210, 235), Color.FromArgb(0, 0, 0), 24, 3 New Font("微软雅黑", 9), 30, Color.FromArgb(250, 250, 250), Color.FromArgb(225, 225, 225), Color.FromArgb(0, 0, 0), 4 "0;80;70;595", "M;M;M;L")
其中列宽的控制通过一个字符串来设置,用分号来分隔数字,各数字依次设置对应列的宽度;列的对齐方式也类似,不过左、中、右分别用字母L/M/R表示。
接下来直接上CGridHelper类模块的代码了,代码还比较粗糙,但是满足我自己的需求就够了。
1 Public Class CGridHelper 2 Private mGrid As DataGridView 3 Public Property Grid() As DataGridView 4 Get 5 Return mGrid 6 End Get 7 Set(ByVal value As DataGridView) 8 mGrid = value 9 mGrid.BorderStyle = BorderStyle.None 10 mGrid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single 11 mGrid.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False 12 mGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing 13 mGrid.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single 14 mGrid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing 15 mGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None 16 mGrid.DefaultCellStyle.WrapMode = DataGridViewTriState.True 17 mGrid.EnableHeadersVisualStyles = False 18 mGrid.AllowUserToResizeRows = False 19 mGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect 20 mGrid.ReadOnly = True 21 mGrid.AllowUserToAddRows = False 22 mGrid.AllowUserToDeleteRows = False 23 For i = 0 To mGrid.Columns.Count - 1 24 mGrid.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable 25 Next 26 End Set 27 End Property 28 29 Private mOddRowBackColor As Color 30 Public Property OddRowBackColor() As Color 31 Get 32 Return mOddRowBackColor 33 End Get 34 Set(ByVal value As Color) 35 mOddRowBackColor = value 36 If mGrid.Rows.Count = 0 Then Exit Property 37 For i = 0 To mGrid.Rows.Count - 1 Step 2 38 mGrid.Rows(i).DefaultCellStyle.BackColor = value 39 Next 40 End Set 41 End Property 42 43 Private mHeaderBackColor As Color 44 Public Property HeaderBackColor() As Color 45 Get 46 Return mHeaderBackColor 47 End Get 48 Set(ByVal value As Color) 49 mHeaderBackColor = value 50 mGrid.ColumnHeadersDefaultCellStyle.BackColor = value 51 End Set 52 End Property 53 54 Private mEvenRowBackColor As Color 55 Public Property EvenRowBackColor() As Color 56 Get 57 Return mEvenRowBackColor 58 End Get 59 Set(ByVal value As Color) 60 mEvenRowBackColor = value 61 If mGrid.Rows.Count = 0 Then Exit Property 62 For i = 1 To mGrid.Rows.Count - 1 Step 2 63 mGrid.Rows(i).DefaultCellStyle.BackColor = value 64 Next 65 End Set 66 End Property 67 68 Private mGridColor As Color 69 Public Property GridColor() As Color 70 Get 71 Return mGridColor 72 End Get 73 Set(ByVal value As Color) 74 mGridColor = value 75 mGrid.GridColor = value 76 End Set 77 End Property 78 79 Private mColumnWidth As String 80 Public Property ColumnWidth() As String 81 Get 82 Return mColumnWidth 83 End Get 84 Set(ByVal value As String) 85 mColumnWidth = value 86 Dim mColCount As Integer = mGrid.ColumnCount 87 Dim mWidths() As String = value.Split(";") 88 For i = 0 To mWidths.Length - 1 89 If i + 1 > mColCount Then Exit For 90 If mWidths(i) <= 0 Then 91 mGrid.Columns(i).Visible = False 92 Else 93 mGrid.Columns(i).Width = mWidths(i) 94 End If 95 Next 96 End Set 97 End Property 98 99 Private mColumnAlignment As String 100 Public Property ColumnAlignment() As String 101 Get 102 Return mColumnAlignment 103 End Get 104 Set(ByVal value As String) 105 mColumnAlignment = value 106 Dim mColCount As Integer = mGrid.ColumnCount 107 Dim mAlignments() As String = value.Split(";") 108 For i = 0 To mAlignments.Length - 1 109 If i + 1 > mColCount Then Exit For 110 Select Case mAlignments(i) 111 Case Is = "L" 112 mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft 113 Case Is = "R" 114 mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight 115 Case Is = "M" 116 mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter 117 Case Else 118 mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft 119 End Select 120 Next 121 End Set 122 End Property 123 124 Private mHeaderFont As Font 125 Public Property HeaderFont() As Font 126 Get 127 Return mHeaderFont 128 End Get 129 Set(ByVal value As Font) 130 mHeaderFont = value 131 mGrid.ColumnHeadersDefaultCellStyle.Font = value 132 End Set 133 End Property 134 135 Private mHeaderHeight As Integer 136 Public Property HeaderHeight() As Integer 137 Get 138 Return mHeaderHeight 139 End Get 140 Set(ByVal value As Integer) 141 mHeaderHeight = value 142 mGrid.ColumnHeadersHeight = value 143 End Set 144 End Property 145 146 Private mHeaderForeColor As Color 147 Public Property HeaderForeColor() As Color 148 Get 149 Return mHeaderForeColor 150 End Get 151 Set(ByVal value As Color) 152 mHeaderForeColor = value 153 mGrid.ColumnHeadersDefaultCellStyle.ForeColor = value 154 End Set 155 End Property 156 157 Private mRowHeaderWidth As Integer 158 Public Property RowHeaderWidth() As Integer 159 Get 160 Return mRowHeaderWidth 161 End Get 162 Set(ByVal value As Integer) 163 mRowHeaderWidth = value 164 mGrid.RowHeadersWidth = value 165 End Set 166 End Property 167 168 Private mRowHeight As Integer 169 Public Property RowHeight() As Integer 170 Get 171 Return mRowHeight 172 End Get 173 Set(ByVal value As Integer) 174 mRowHeight = value 175 For i = 0 To mGrid.Rows.Count - 1 176 mGrid.Rows(i).Height = value 177 Next 178 End Set 179 End Property 180 181 Private mRowFont As Font 182 Public Property RowFont() As Font 183 Get 184 Return mRowFont 185 End Get 186 Set(ByVal value As Font) 187 mRowFont = value 188 mGrid.RowsDefaultCellStyle.Font = value 189 End Set 190 End Property 191 192 Private mBackColor As Color 193 Public Property BackColor() As Color 194 Get 195 Return mBackColor 196 End Get 197 Set(ByVal value As Color) 198 mBackColor = value 199 mGrid.BackgroundColor = value 200 End Set 201 End Property 202 203 Public Sub New(TargetGrid As DataGridView, BackColor As Color, HeaderFont As Font, HeaderHeight As Integer, 204 HeaderBackColor As Color, HeaderForeColor As Color, RowHeaderWidth As Integer, 205 RowFont As Font, RowHeight As Integer, OddRowBackColor As Color, EvenRowBackColor As Color, 206 GridColor As Color, ColumnWidth As String, ColumnAlignment As String) 207 Me.Grid = TargetGrid 208 Me.BackColor = BackColor 209 210 Me.HeaderFont = HeaderFont 211 Me.HeaderHeight = HeaderHeight 212 Me.HeaderBackColor = HeaderBackColor 213 Me.HeaderForeColor = HeaderForeColor 214 Me.RowHeaderWidth = RowHeaderWidth 215 216 Me.RowFont = RowFont 217 Me.RowHeight = RowHeight 218 Me.OddRowBackColor = OddRowBackColor 219 Me.EvenRowBackColor = EvenRowBackColor 220 221 Me.GridColor = GridColor 222 Me.ColumnWidth = ColumnWidth 223 Me.ColumnAlignment = ColumnAlignment 224 End Sub 225 226 Public Sub ReSetStyle() 227 ReSetStyle(Me.BackColor, Me.HeaderFont, Me.HeaderHeight, Me.HeaderBackColor, Me.HeaderForeColor, Me.RowHeaderWidth, 228 Me.RowFont, Me.RowHeight, Me.OddRowBackColor, Me.EvenRowBackColor, Me.GridColor, Me.ColumnWidth, Me.ColumnAlignment) 229 End Sub 230 231 Private Sub ReSetStyle(BackColor As Color, HeaderFont As Font, HeaderHeight As Integer, 232 HeaderBackColor As Color, HeaderForeColor As Color, RowHeaderWidth As Integer, 233 RowFont As Font, RowHeight As Integer, OddRowBackColor As Color, EvenRowBackColor As Color, 234 GridColor As Color, ColumnWidth As String, ColumnAlignment As String) 235 Me.BackColor = BackColor 236 237 Me.HeaderFont = HeaderFont 238 Me.HeaderHeight = HeaderHeight 239 Me.HeaderBackColor = HeaderBackColor 240 Me.HeaderForeColor = HeaderForeColor 241 Me.RowHeaderWidth = RowHeaderWidth 242 243 Me.RowFont = RowFont 244 Me.RowHeight = RowHeight 245 Me.OddRowBackColor = OddRowBackColor 246 Me.EvenRowBackColor = EvenRowBackColor 247 248 Me.GridColor = GridColor 249 Me.ColumnWidth = ColumnWidth 250 Me.ColumnAlignment = ColumnAlignment 251 End Sub 252 End Class
以上是关于关于WinForm下DataGridView控件行的选择的问题的主要内容,如果未能解决你的问题,请参考以下文章
C#WinForm datagridview控件取消最左边存储行标题的列
关于Winform下DataGridView中实现checkbox全选反选同步列表项的处理
winForm如何实现ListView控件中数据的换行显示?