我有一个代码在datagridview
显示记录和过滤数据时有效。
ShowRecord代码:
Using cmd As New SqlClient.SqlCommand("dbo.uspSELECTALL", cn)
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)
dgv1.RowTemplate.Height = 30
dgv1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
dgv1.ColumnCount = 8
SelectedRows()
dgv1.AutoGenerateColumns = False
dgv1.Columns(0).Name = "Student ID"
dgv1.Columns(0).DataPropertyName = "StudentID"
dgv1.Columns(1).Name = "Last Name"
dgv1.Columns(1).DataPropertyName = "SurName"
dgv1.Columns(2).Name = "First Name"
dgv1.Columns(2).DataPropertyName = "FirstName"
dgv1.Columns(3).Name = "Middle Name"
dgv1.Columns(3).DataPropertyName = "MiddleName"
dgv1.Columns(4).Name = "Gender"
dgv1.Columns(4).DataPropertyName = "Gender"
dgv1.Columns(5).Name = "Birthday"
dgv1.Columns(5).DataPropertyName = "Birthday"
dgv1.Columns(6).Name = "Age"
dgv1.Columns(6).DataPropertyName = "Age"
dgv1.Columns(7).Name = "ContactNumber"
dgv1.Columns(7).DataPropertyName = "TelNum"
dgv1.DataSource = dt
End Using
FilterData代码:
Using cmd As New SqlClient.SqlCommand("dbo.uspYearFilter", cn)
cmd.Parameters.AddWithValue("@Year", cboYear.Text)
cmd.CommandType = CommandType.StoredProcedure
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)
dgv1.RowTemplate.Height = 30
dgv1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
dgv1.AutoGenerateColumns = False
dgv1.Columns(0).Name = "Student ID"
dgv1.Columns(0).DataPropertyName = "StudentID"
dgv1.Columns(1).Name = "Last Name"
dgv1.Columns(1).DataPropertyName = "SurName"
dgv1.Columns(2).Name = "First Name"
dgv1.Columns(2).DataPropertyName = "FirstName"
dgv1.Columns(3).Name = "Middle Name"
dgv1.Columns(3).DataPropertyName = "MiddleName"
dgv1.Columns(4).Name = "Gender"
dgv1.Columns(4).DataPropertyName = "Gender"
dgv1.Columns(5).Name = "Birthday"
dgv1.Columns(5).DataPropertyName = "Birthday"
dgv1.Columns(6).Name = "Age"
dgv1.Columns(6).DataPropertyName = "Age"
dgv1.Columns(7).Name = "ContactNumber"
dgv1.Columns(7).DataPropertyName = "TelNum"
dgv1.DataSource = dt
End Using
这里的问题是当我使用FilterData代码并再次选择ShowRecord代码时。它在这一行引发错误:
dgv1.ColumnCount = 8
说
无法在数据绑定的datagridview上设置columncount属性 控制
任何人都可以帮我解决这个问题。感谢