如何在当前场景中将用户的 ID 替换为他们的全名?

How to replace IDs of users with their fullNames in the current scenario?

我有两个具有以下架构的数据库:

tbl_users(userID, fullName, emailID, password)

userID是主键

tbl_savings(savingsID, creditorName, amount, interestRate, interestType, interestCalculationMethod, ownerID, dataEnteredByID) 

savingsID 是主键。

ownerID 是 tbl_users 的 userID 上的外键,具有一对多关系。

ownerID 指定保存所有者的 ID。同样,dataEnteredByID 指定输入数据的人的 ID。

我有一个具有以下规格的 GridView:

<asp:GridView ID="GridViewSavingsTracker" AutoGenerateColumns="false" runat="server" DataKeyNames="savingsID" OnRowCommand="GridViewSavingsTracker_RowCommand" AllowPaging="true" OnSelectedIndexChanged="GridViewSavingsTracker_SelectedIndexChanged" OnPageIndexChanging="GridViewSavingsTracker_PageIndexChanging" PageSize="10">
    <Columns>
        <asp:CommandField ShowSelectButton="true" />
        <asp:BoundField DataField="creditorName" HeaderText="Creditor Name" />
        <asp:BoundField DataField="amount" HeaderText="Principal Amount" />
        <asp:BoundField DataField="interestRate" HeaderText="Interest Rate" />
        <asp:BoundField DataField="interestType" HeaderText="Interest Type" />
        <asp:BoundField DataField="interestCalculationMethod" HeaderText="Interest Calculation Method" />
        <asp:BoundField DataField="insertedDate" HeaderText="Date" />
        <asp:BoundField DataField="ownerID" HeaderText="Owner" />
        <asp:BoundField DataField="dataEnteredByID" HeaderText="Data Entered By" />

    </Columns>
</asp:GridView>

我想做的事情: 我不想打印 ownerID 和 dataEnteredByID,而是打印相应 ID 的全名。我该怎么做?

我已有的: 我有以下 GridView 服务器端代码。我有一个方法 ShowGridView() 将数据从数据库填充到 GridView 中。

protected void ShowGridView()
{
    string connectionString = GetConnString();
    OleDbConnection con = new OleDbConnection(connectionString);
    con.Open();

    string showGridViewQuery = "(SELECT s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName FROM tbl_savings s LEFT JOIN tbl_users u ON s.ownerID = u.usersID) UNION (select s.creditorName, s.amount, s.interestRate, s.interestType, s.interestCalculationMethod, s.insertedDate, u.fullName from tbl_savings s LEFT JOIN tbl_users u ON s.dataEnteredByID = u.usersID)";

    OleDbCommand showGridViewCommand = new OleDbCommand(showGridViewQuery, con);
    showGridViewCommand.ExecuteNonQuery();
    DataTable dt = new DataTable();
    OleDbDataAdapter olda = new OleDbDataAdapter(showGridViewCommand);
    olda.Fill(dt);

    GridViewSavingsTracker.DataSource = dt;
    GridViewSavingsTracker.DataBind();

    con.Close();
}

您可以创建一个单独的代码隐藏方法来获取全名并从 gridview 调用它。

为该字段创建一个 ItemTemplate。 Text 属性 调用该方法。将 UserID 替换为您的字段名称。

<ItemTemplate>
    <asp:Label ID="lblUserNameItem" runat="server" 
        Text='<%# GetUserNameByID(Eval("UserID").ToString()) %>'></asp:Label>
</ItemTemplate>

后面的代码:

protected string GetUserNameByID(string userID)
{
    // your data-code...
    ... uname = GetUserByID(int.Parse(userID));
    return uname;
}

用同样的方法或其他方法对另一个id做同样的事情。