我的代码使用sqldataadapter和dataset填充DataGrid控件(vb.net 3.5框架),并通过其他控件的组合进行筛选。一切正常,除了RadioButtonList1在一个条件下有3个rbtns:
选择单选按钮1 @ index 0后,如果先前选择了rbtn 3 @ index 2并且它正确返回NO DATA,则会出现问题。
当先前选择了rbtn 3 @ index 2且它正确返回了SOME DATA时,不会发生这种情况。 radiobuttonlist选择的所有其他组合表现正确
我尝试使用sqldataadapter以不同的方式填充datagrid - 请参阅代码,包括使用存储过程(但如果我这样做,我无法自定义排序)。似乎没有什么能改变这种奇怪的行为。
我可能不得不求助于将单选按钮1更改为复选框控件并使用单独的绑定方法,如果没有人可以帮助我看到光。任何人都可以看到我遗失的任何明显事物吗?
ASPX(为简洁起见,datagrid仅显示2列)
<asp:radiobuttonlist id="RadioButtonList1" runat="server" AutoPostBack="True"
Font-Names="Arial"
Font-Size="X-Small"
BorderColor="Black"
ForeColor="Black"
RepeatDirection="Vertical"
TextAlign="Right" CssClass="radioWithProperWrap" RepeatColumns="1">
<asp:ListItem Value="0" >All Projects</asp:ListItem>
<asp:ListItem Value="1" Selected="True" >Sort By Project#</asp:ListItem>
<asp:ListItem Value="2">Sort By Project Name</asp:ListItem>
</asp:radiobuttonlist>
<!--------------------
DataGrid1
-------------------->
<asp:datagrid id="DataGrid1" runat="server"
Font-Names="Tahoma,Sans Serif,arial"
Font-Size="Small"
ShowFooter="True"
DataMember="vwPersonSummary"
AllowSorting="True"
OnPageIndexChanged="DataGrid1_Paging" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
UseAccessibleHeader="True" AutoGenerateColumns="False"
CssClass="nowrap" AllowPaging="True" PageSize="110" Visible="False" >
<FooterStyle HorizontalAlign="Right" Font-Bold="True" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"></FooterStyle>
<SelectedItemStyle HorizontalAlign="Left" />
<PagerStyle HorizontalAlign="Left" Mode="NumericPages"
Position="TopAndBottom" PageButtonCount="75" />
<AlternatingItemStyle BackColor="#EBF5FF"></AlternatingItemStyle>
<headerStyle Font-Bold="True" Font-Italic="False" Font-Overline="False"
Font-Size="Small" Font-Strikeout="False" Font-Underline="False"
Width="12em" BackColor="#333333" ForeColor="White" ></headerStyle>
<ItemStyle Font-Size="Smaller" Width="12em" />
<Columns>
<asp:TemplateColumn HeaderText="Line#" FooterText="Line#">
<FooterStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center"
Font-Names="Tahoma" Font-Size="Small" ForeColor="Black" />
<headerStyle HorizontalAlign="Center" Font-Bold="True" Font-Italic="False"
Font-Names="Tahoma" Font-Overline="False" Font-Size="Small"
Font-Strikeout="False" Font-Underline="False" ForeColor="White"></headerStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
<ItemTemplate>
<%#(DataGrid1.PageSize * DataGrid1.CurrentPageIndex) + Container.ItemIndex + 1%> <!--generate row numbers-->
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Person" SortExpression="Employee" HeaderText="Person" >
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
<headerStyle HorizontalAlign="Center" Font-Bold="True" Font-Italic="False"
Font-Overline="False" Font-Size="Small" Font-Strikeout="False"
Font-Underline="False" Wrap="False" Font-Names="Tahoma"
ForeColor="White" > </headerStyle>
<ItemStyle Font-Bold="False" Font-Italic="False"
Font-Overline="False" Font-Strikeout="False" Font-Underline="False"
HorizontalAlign="Left" Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Project#" SortExpression="Project#" HeaderText="Proj#">
<FooterStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" Wrap="False" />
<headerStyle HorizontalAlign="Center" Font-Bold="True"
Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" Wrap="False" Font-Names="Tahoma" Font-Size="Small"
ForeColor="White" ></headerStyle>
<ItemStyle Font-Bold="False" Font-Italic="False" Font-Overline="False"
Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Left"
Wrap="False" />
</asp:BoundColumn>
</Columns>
</asp:datagrid>
ASPX.VB(仅限相关代码)
Sub BindData(ByVal strSortField As String)
''clear grid
DataGrid1.DataSource = Nothing 'original
' TimeCodesSelected variable is populated based on Checkbox selections here.
' Code removed for brevity.
Try
' Selected project
Dim ProjectSelected As String = Left(ddwnProjList.SelectedItem.Text, 7) 'Project#
' convert string dates to DateTime data types
PPBegDate = Convert.ToDateTime(txtBegDate.Text)
PPEndDate = Convert.ToDateTime(txtEndDate.Text)
' Check which radio button is selected
If RadioButtonList1.SelectedValue = 0 Then '0 = All projects
ProjectSelected = String.Empty
'inline sql used to provide sort parameters to allow retaining sort order when paging through grid. sqlQuery is a global variable
sqlQuery = "SELECT Person, Project# WHERE dtmDate BETWEEN '" & PPBegDate & "' AND '" & PPEndDate & "' " & TimeCodesSelected & " ORDER BY " & ViewState("sortField").ToString() & " " & ViewState("sortDirection").ToString()
''1=by number, or 2=by name
Else
sqlQuery = "SELECT Person, Project#, WHERE Project#='" & ProjectSelected & "' AND dtmDate BETWEEN '" & PPBegDate & "' AND '" & PPEndDate & "' " & TimeCodesSelected & " ORDER BY " & ViewState("sortField").ToString() & " " & ViewState("sortDirection").ToString()
'Verify selected project exists, if not hide grid
Dim blnRecordExists As Boolean
Dim oRecord As DataAccessLib.SqlConn = New DataAccessLib.SqlConn()
blnRecordExists = oRecord.VerifyProjectExists(ProjectSelected, PPBegDate, PPEndDate)
If (blnRecordExists = True) Then
DataGrid1.Visible = True
lblErrorMsg.Text = Nothing
Else
DataGrid1.Visible = False
lblErrorMsg.Text = "No records found for this project number during selected dates."
End If
End If
Using conn As SqlConnection = New SqlConnection(conStr)
Using cmd As New SqlCommand(sqlQuery, conn)
cmd.CommandType = CommandType.Text
Using sda As New SqlDataAdapter(cmd)
Using ds As New DataSet()
sda.Fill(ds)
DataGrid1.DataSource = ds.Tables(0)
End Using
End Using
End Using
''Orignal code that works as well and also has the switch from index 2 to index 0 issue
'SqlDataAdapter1.Fill(DsDetailByProj1, "vwPersonSummary")
'Dim objDataView As DataView = DsDetailByProj1.Tables("vwPersonSummary").DefaultView
'DataGrid1.DataSource = objDataView
DataGrid1.DataBind()
End Using 'end using closes conn and goes before 'Catch'
Catch ex As SqlException
sqlErrorMsg = sqlErrorMsg + ": " + ex.Message.ToString
lblErrorMsg.Visible = True
lblErrorMsg.Text = sqlErrorMsg
Catch e As Exception
'reset grid to 1st page after grid refresh (databind)
DataGrid1.CurrentPageIndex = 0
Finally 'the using statement closes objects. Kept here for reference only.
''close and dispose of database connection
'SqlConnection1.Close()
'SqlConnection1.Dispose()
''close sqldatadapter if applicable
'SqlDataAdapter1.Dispose()
End Try
End Sub
Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButtonList1.SelectedIndexChanged
If RadioButtonList1.SelectedValue = 0 Then
ddwnProjList.Enabled = False
Else 'if 1 or 2
ddwnProjList.Enabled = True
End If
GetProjectList()'fills ddwnProjList by Project Number or Project Name
BindData("dtmDate")
End Sub
Private Sub ddwnProjList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddwnProjList.SelectedIndexChanged
If (ViewState("sortField") <> Nothing) Then
BindData(ViewState("sortField").ToString())
Else
BindData("dtmDate")
End If
End Sub
enter code here
答案 0 :(得分:0)
正如我在评论中提到的,将DataGrid的visibility属性设置为true是解决此问题的第一步。
然后我使用Scott Mitchell的回答here来验证网格是否为空。通过消除对数据库的另一次往返的需要,这似乎比我的方式更有效。
If (DataGrid1.Items.Count = 0) Then
lblNoRecordsMessage.Text = "Yo Adrian, there are no records for the selected dates."
lblNoRecordsMessage.Visible = True
DataGrid1.Visible = False
Else
lblNoRecordsMessage.Text = Nothing
lblNoRecordsMessage.Visible = False
DataGrid1.Visible = True
End If
我还确保DataBrid和Label在DataBind()方法的开头设置为可见,因为在最后一次数据绑定之后它有可能被置于不可见状态。
lblNoRecordsMessage.Text = Nothing
lblNoRecordsMessage.Visible = False
DataGrid1.Visible = True