将查询结果关联到特定单元格下的datagridview

Associate results of query to datagridview under specific cell

您好,我有一个数据网格视图,我想在特定字段“MEASUREMENT”下指定查询结果

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-9UN2C31;Initial Catalog=projectdatabase;Integrated Security=True");
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT nome_misurazione FROM measurements INNER JOIN projectmeasurement ON measurements.id_misurazione = projectmeasurement.id_misuraz INNER JOIN project ON project.id_progetto = projectmeasurement.id_progetto INNER JOIN login ON project.id_login = login.id WHERE project.id_progetto = @id_progetto";
    cmd.Parameters.AddWithValue("@id_progetto", myproject.v);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    con.Close();

}

这里有查询结果,我想将其添加到datagridview 中的特定字段调用MEASUREMENT 下。网格视图代码:

         <div align ="center"style="margin-top:100px;">
             <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">
                 <Columns>
                     <asp:BoundField HeaderText="MEASUREMENT" />
                     <asp:ButtonField ButtonType="Button" Text="VIEW">
                     <ControlStyle BackColor="Red" />
                     </asp:ButtonField>
                     <asp:ButtonField ButtonType="Button" Text="DELETE">
                     <ControlStyle BackColor="Red" />
                     </asp:ButtonField>
                 </Columns>
                 <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                 <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                 <RowStyle BackColor="White" ForeColor="#330099" />
                 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                 <SortedAscendingCellStyle BackColor="#FEFCEB" />
                 <SortedAscendingHeaderStyle BackColor="#AF0101" />
                 <SortedDescendingCellStyle BackColor="#F6F0C0" />
                 <SortedDescendingHeaderStyle BackColor="#7E0000" />
             </asp:GridView>
             <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
         </div>

好吧,如果测量值是某个 PK 值,那么只需将其加入您的基本查询即可。

换句话说,它可能是一个值,所以一个简单的连接就可以解决问题。

我的意思是,我可能会这样说:

SELECT ID, FirstName, LastName, Hotel_ID from People

现在,我当然不想显示来自 tblHotels 的 Hotel_ID,但我想要酒店名称,所以我会像这样构建查询:

请注意我们如何使用 LEFT JOIN,因为 Hotel_ID 可能为空(未设置)。

实际上,上面的简单查询是对 Hotel_ID 的查找,现在我们可以使用漂亮的文本名称代替 ID。

SELECT        People.ID, People.Firstname, People.LastName, tblHotels.HotelName
FROM            People LEFT  JOIN
                         tblHotels ON People.Hotel_ID = tblHotels.ID

现在,您可以 运行 通过使用数据行绑定事件对每一行进行完整的 sql 查询,但我认为我们不需要这样做。