我从数据库查询加载单个数据表。然后我想将数据表分配给数据网格。数据表加载正常,但数据网格不加载。我在其他代码中这样做,它工作正常。我不明白为什么它在这里不起作用。
XAML代码:
<DataGrid
IsReadOnly="True"
Name="dtCIDGrid"
Background="#FFE8F9FF"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="150"
Margin="24,66,0,0"
Width="240"
BorderThickness="1"
BorderBrush="Black"
>
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger
Property="DataGridCell.IsSelected"
Value="True">
<Setter Property="Background" Value="#FF9DF3D6" />
<Setter Property="Foreground" Value="#000000" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
这是我的c#代码:
private void LoadCardIssueDates(string strClientID, SqlConnection sqlConn, string strErrorLogFile)
{
bool blnSuccess = false;
DataTable dtCIDData = new DataTable();
dtCIDData.Columns.Add("Card Issue Date", typeof(string));
blnSuccess = GetCardIssueDates(strClientID, sqlConn, dtCIDData, strErrorLogFile);
}
private bool GetCardIssueDates(string strClientID, SqlConnection sqlConn, DataTable dtCIDData, string strErrorLogFile)
{
string strCardIssueDate = string.Empty;
string[] strDatePieces = new string[3];
DateTime dtCardIssueDate = Convert.ToDateTime("01/01/1900");
bool blnSuccess = false;
if (sqlConn != null && sqlConn.State == ConnectionState.Open)
{
try
{
using (SqlCommand sqlCmd = new SqlCommand("GetCardIssueDates", sqlConn))
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add("@ClientID", SqlDbType.Char, 36);
// set parameter values that your code will send to the SP as parameter values
sqlCmd.Parameters["@ClientID"].Value = strClientID;
using (SqlDataReader sdrData = sqlCmd.ExecuteReader())
{
while (sdrData.Read())
{
if (sdrData.IsDBNull(0))
{
strCardIssueDate = string.Empty;
}
else
{
dtCardIssueDate = (DateTime)sdrData["issuedate"];
strCardIssueDate = dtCardIssueDate.ToString("MM/dd/yyyy");
}
dtCIDData.Rows.Add(strCardIssueDate);
}
dtCIDGrid.ItemsSource = dtCIDData.DefaultView;
sdrData.Close();
}
sqlCmd.Clone();
sqlCmd.Dispose();
blnSuccess = true;
}
blnSuccess = true;
}
catch (SqlException sqlex)
{
WriteCIDErrorLog("InsertCardIssueDate", sqlex.Message, strErrorLogFile, false);
System.Windows.MessageBox.Show("InsertCardIssueDate: SQL Error, " + sqlex.Message + ", has occurred.");
blnSuccess = false;
}
catch (InvalidOperationException invex)
{
WriteCIDErrorLog("InsertCardIssueDate", invex.Message, strErrorLogFile, false);
System.Windows.MessageBox.Show("InsertCardIssueDate: Invalid Operation Error, " + invex.Message + ", has occurred.");
blnSuccess = false;
}
catch (Exception exQuery)
{
WriteCIDErrorLog("InsertCardIssueDate", exQuery.Message, strErrorLogFile, false);
System.Windows.MessageBox.Show("InsertCardIssueDate: Exception Error, " + exQuery.Message + ", has occurred.");
blnSuccess = false;
}
}
else
{
WriteCIDErrorLog("InsertCardIssueDate", "There is no database connection.", strErrorLogFile, false);
System.Windows.MessageBox.Show("InsertCardIssueDate: There is no database connection.");
}
return blnSuccess;
}