在当前情况下如何用用户的全名替换用户的ID? [英] How to replace IDs of users with their fullNames in the current scenario?

查看:95
本文介绍了在当前情况下如何用用户的全名替换用户的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two databases with the following schema:

tbl_users(userID, fullName, emailID, password)

用户ID 是主键

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

SavingsID 是主键.

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

ownerID is a Foreign Key on userID of tbl_users with a one-to-many relationship.

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

ownerID specifies the ID of the owner of the saving. Similarly, dataEnteredByID specifies the ID of the person who entered the data.

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

I have a GridView with the following specification:

<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的全名.我该怎么办?

What I want to do: Instead of printing the ownerID and dataEnteredByID, I want to print the fullNames of the corresponding IDs. How can I do that?

我已经拥有的东西:我有以下GridView的服务器端代码.我有一个ShowGridView()方法,可将数据从数据库填充到GridView中.

What I already have: I have the following server side code for GridView. I have a method ShowGridView() that populates the data into the GridView from the database(s).

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调用它.

You can create a separate code-behind method that gets the full name and call it from the gridview.

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

Create an ItemTemplate for the field. The Text property calls the method. Replace UserID with your field's name.

<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进行相同的操作,或使用相同的方法.

Do the same with the other id, with the same method or another.

这篇关于在当前情况下如何用用户的全名替换用户的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆