Home > OS >  Associate results of query to datagridview under specific cell
Associate results of query to datagridview under specific cell

Time:02-08

Hi i've a datagridview and i want to specify the results of query under the specific field "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();

}

Here there is the query result and i want to add it under specific field call MEASUREMENT in datagridview. Gridview Code:

         <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>

CodePudding user response:

Well, if measurement is some PK value, then just left join it in base query you have.

In other words, it probably one value, so a simple join should do the trick.

I mean, I might have say this:

SELECT ID, FirstName, LastName, Hotel_ID from People

Now, of couse I don't want to display Hotel_ID from tblHotels, but I want the Hotel Name, so I would build the query like this:

enter image description here

Note how we use a LEFT JOIN, since the Hotel_ID might be blank (not set).

In effect, the simple above query is a lookup for the Hotel_ID, and now we can use the nice text name in place of the ID.

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

Now, you CAN RUN a whole sql query for each row by using the data row bound event, but I don't think we need to do that.

  •  Tags:  
  • Related