在asp.net中建立gridview中数据之间的关系

发布于 2024-12-15 18:18:53 字数 1063 浏览 1 评论 0原文

我有一种方法,可以使用 sql 表中的数据填充数据表,而该数据表又将用于填充 gridview。现在,在 sql 表中,我有一个名为“hotel”的字段,其中包含酒店的“ID”,该字段与另一个名为“hotels”的表(包含酒店 ID 和名称)相关。

现在,在我的网格视图中,我想显示酒店名称而不是酒店 ID。我该怎么办呢。

 public static DataTable GetRequests(string empid)
        {
            DataTable dt = new DataTable();
            string strConnection = ConfigurationManager.AppSettings["connStr"];
            using (SqlConnection connection = new SqlConnection(strConnection))
            {
                connection.Open();
                SqlCommand sqlcmd = new SqlCommand();
                SqlDataAdapter sAdap = new SqlDataAdapter();                
                sqlcmd.Connection = connection;
                sqlcmd.CommandType = System.Data.CommandType.Text;
                sqlcmd.CommandText = "Select request_date,hotel,dining_date,status from requests Where emp_id='" + empid + "'";
                sAdap.SelectCommand = sqlcmd;
                sAdap.Fill(dt);
            }
            return dt;            
        }

这是检索记录的方法。酒店字段包含我想要其名称的 ID。

i have a method that will populate a datatable with the data from a sql table which in turn would be used to populate a gridview. Now in the sql table i have a field called "hotel" which contains the "ID" of a hotel which relates to another table called "hotels" with hotel id and name.

Now in my grid view i want to display the hotel name instead of the hotel id. How can i do it.

 public static DataTable GetRequests(string empid)
        {
            DataTable dt = new DataTable();
            string strConnection = ConfigurationManager.AppSettings["connStr"];
            using (SqlConnection connection = new SqlConnection(strConnection))
            {
                connection.Open();
                SqlCommand sqlcmd = new SqlCommand();
                SqlDataAdapter sAdap = new SqlDataAdapter();                
                sqlcmd.Connection = connection;
                sqlcmd.CommandType = System.Data.CommandType.Text;
                sqlcmd.CommandText = "Select request_date,hotel,dining_date,status from requests Where emp_id='" + empid + "'";
                sAdap.SelectCommand = sqlcmd;
                sAdap.Fill(dt);
            }
            return dt;            
        }

This is the method that retrieves the records. The hotel field contains the ID for which i want the name.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泡沫很甜 2024-12-22 18:18:53

更改你的 sql 查询,事情就会解决。

假设“Hotel”包含酒店 ID,另一个表“HotelDetails”包含酒店 ID 和酒店名称。

酒店表结构
酒店ID int
HotelDetails的表结构
酒店ID int,
HotelName varchar(10)

现在您的查询应该是

SELECT b.HotelName as HotelName, c.request_date as RequestDate,c.dining_date as DiningDate ,c.status as Status FROM Hotel a, HotelDetails b, requests c WHERE a.HotelId = b.HotelId and emp_id='" + empid + "'

在 GridView 中,您的 DataFild 应该是 HotelName 以便显示酒店名称

例如

<Columns>
                                                            <asp:TemplateField HeaderText="Slno">
                                                                <ItemTemplate>
                                                                    <%# Container.DataItemIndex + 1 %>
                                                                </ItemTemplate>
                                                                <ControlStyle Width="30px" />
                                                                <ItemStyle ForeColor="#00846F" Width="30px" />
                                                            </asp:TemplateField>
                                                            <asp:BoundField ControlStyle-Width="90" DataField="HotelName" 
                                                                HeaderText="Hotel Name" ItemStyle-ForeColor="#00846F" 
                                                                ItemStyle-Width="30">
                                                                <ControlStyle Width="30px" />
                                                                <ItemStyle Width="30px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="190" 
                                                                DataField="RequestDate" HeaderText="Request Date" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="100">
                                                                <ControlStyle Width="100px" />
                                                                <ItemStyle Width="100px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="100" 
                                                                DataField="DiningDate" HeaderText="Dining Date" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="50">
                                                                <ControlStyle Width="50px" />
                                                                <ItemStyle Width="50px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="100" 
                                                                DataField="Status" HeaderText="Status" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="50">
                                                                <ControlStyle Width="50px" />
                                                                <ItemStyle Width="50px" />
                                                            </asp:BoundField>



                                                        </Columns>

如果您发现它有用,请将其标记为您的答案,否则请告诉我......

Change to your sql query and thing will work out.

Let us say 'Hotel' which contain hotel id and in the another table 'HotelDetails which contain the hotel id and hotel name.

Table structure of Hotel
HotelId int
Table structure of HotelDetails
HotelId int,
HotelName varchar(10)

Now you query should be

SELECT b.HotelName as HotelName, c.request_date as RequestDate,c.dining_date as DiningDate ,c.status as Status FROM Hotel a, HotelDetails b, requests c WHERE a.HotelId = b.HotelId and emp_id='" + empid + "'

In the GridView, your DataFild should be HotelName in order to display Hotel Name

For instance

<Columns>
                                                            <asp:TemplateField HeaderText="Slno">
                                                                <ItemTemplate>
                                                                    <%# Container.DataItemIndex + 1 %>
                                                                </ItemTemplate>
                                                                <ControlStyle Width="30px" />
                                                                <ItemStyle ForeColor="#00846F" Width="30px" />
                                                            </asp:TemplateField>
                                                            <asp:BoundField ControlStyle-Width="90" DataField="HotelName" 
                                                                HeaderText="Hotel Name" ItemStyle-ForeColor="#00846F" 
                                                                ItemStyle-Width="30">
                                                                <ControlStyle Width="30px" />
                                                                <ItemStyle Width="30px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="190" 
                                                                DataField="RequestDate" HeaderText="Request Date" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="100">
                                                                <ControlStyle Width="100px" />
                                                                <ItemStyle Width="100px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="100" 
                                                                DataField="DiningDate" HeaderText="Dining Date" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="50">
                                                                <ControlStyle Width="50px" />
                                                                <ItemStyle Width="50px" />
                                                            </asp:BoundField>
                                                            <asp:BoundField ControlStyle-ForeColor="#00846F" ControlStyle-Width="100" 
                                                                DataField="Status" HeaderText="Status" 
                                                                ItemStyle-ForeColor="#00846F" ItemStyle-Width="50">
                                                                <ControlStyle Width="50px" />
                                                                <ItemStyle Width="50px" />
                                                            </asp:BoundField>



                                                        </Columns>

If you find it useful, please mark it as your answer else let me know....

放手` 2024-12-22 18:18:53

您必须使用联接

You have to use Joins.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文