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

对着背影说爱祢 提交于 2020-01-06 06:46:25

问题


I have two databases with the following schema:

tbl_users(userID, fullName, emailID, password)

userID is the Primary Key

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

savingsID is the Primary Key.

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

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

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>

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?

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();
}

回答1:


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

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>

Code behind:

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

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



来源:https://stackoverflow.com/questions/47702144/how-to-replace-ids-of-users-with-their-fullnames-in-the-current-scenario

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!