0

我有一个类似的 SQL 输出

TotalLeave    Status
----------    ------
    3        PaidLeave
    5        MedicalLave

我需要在我的标签控件中显示这个值,比如,

lblMedicalLeave.text = 5
lblPaidLeave.text    = 3

为此,我刚刚在我的代码中为我的数据集创建了对象,例如,

StaffAttendanceStatusTableAdapters.StaffTypesTableAdapter staffAttendanceStatus = 
                  new StaffAttendanceStatusTableAdapters.StaffTypesTableAdapter();
StaffAttendanceStatus.StaffTypesDataTable StaffDataTable = 
               staffAttendanceStatus.GetDataStaffAttendanceStatus(staff.StaffId);

        if (StaffDataTable[0] != null)
        {
            StaffAttendanceStatus.StaffTypesRow StaffRow = StaffDataTable[0];

            lblTotalMedicalLeave.Text = StaffRow.TotalLeave.ToString();
            lblTotalPaidLeave.Text = StaffRow.TotalLeave.ToString();
        }

它显示相同的值(3),是否可以获得相应状态的 TotalLeave 值?有人能帮我一下吗

4

2 回答 2

2

您两次都访问同一行。使用StaffDataTable[1]访问第二行。无论如何,您应该在访问任何值之前检查是否有结果!

using StaffAttendanceStatusTableAdapters;
....


StaffTypesTableAdapter staffAttendanceStatus = new StaffTypesTableAdapter();
StaffAttendanceStatus.StaffTypesDataTable StaffDataTable = 
           staffAttendanceStatus.GetDataStaffAttendanceStatus(staff.StaffId);

if (StaffDataTable != null && StaffDataTable.Count > 1)
{
    lblTotalMedicalLeave.Text = StaffDataTable[0].TotalLeave.ToString();
    lblTotalPaidLeave.Text = StaffDataTable[1].TotalLeave.ToString();
}

hth

于 2012-05-21T05:57:12.143 回答
1

由于需要从两行获取 TotalLeave,因此需要从两行获取数据。

if (StaffDataTable != null && StaffDataTable.Rows.Count > 1)
{
  StaffAttendanceStatus.StaffTypesRow StaffRow1 = StaffDataTable[0];
  StaffAttendanceStatus.StaffTypesRow StaffRow2 = StaffDataTable[1];

  lblTotalMedicalLeave.Text = StaffRow1.TotalLeave.ToString();
  lblTotalPaidLeave.Text = StaffRow2.TotalLeave.ToString();
}

如果没有 PaidLeave 和 MedicalLave 状态的顺序,只需检查 row.Status 并将总值分配给相应的标签

于 2012-05-21T05:14:58.153 回答