我有两个具有以下架构的数据库:
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>
我想做什么:我想打印相应ID的fullNames,而不是打印ownerID和dataEnteredByID。我怎么能这样做?
我已经拥有的内容:我有以下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();
}
答案 0 :(得分:0)
您可以创建一个单独的代码隐藏方法,该方法获取全名并从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执行相同的操作。