简体   繁体   中英

Grouping gridview with SQL Server table only add 1 item pr id

I have an issue where it will only add 1 item from the table per id.

I have 2 tables, the first Data.Mærke contains the id and name for the group description and second Data.Hjuldata that contains the id from the Data.Mærke and id for the items and all the items

在此处输入图片说明

Strings

string connStrings = ConfigurationManager.AppSettings["Sql"];

string Data = @"Select ps.Mærket AS SubCategoryName, P.DataID, P.Billed, P.Model, P.Årgang, P.[Motor Type], P.Krydsmål, P.Centerhul, P.Møtrikker, P.Bolter, P.Dæk, P.Fælge from Data.Hjuldata P  inner join Data.Mærke PS on P.DataID = PS.MærkeID";

Code

public MainWindow()
{
    InitializeComponent();
    BindData();

    ICollectionView dataView = CollectionViewSource.GetDefaultView(hjuldata.ItemsSource);

    dataView.GroupDescriptions.Add(new PropertyGroupDescription("SubCategoryName"));
}

private void BindData()
{
    hjuldata.ItemsSource = RetrieveProductsbySubCat().Tables[0].DefaultView;    
}

public DataSet RetrieveProductsbySubCat()
{
    //SQL statement to fetch entries from products
    DataSet dsProducts = new DataSet();

    //Open SQL Connection
    using (SqlConnection conns = new SqlConnection(connStrings))
    {
        conns.Open();

        //Initialize command object
        using (SqlCommand cmds = new SqlCommand(Data, conns))
        {
            SqlDataAdapter adapters = new SqlDataAdapter(cmds);

            //Fill the result set
            adapters.Fill(dsProducts);
        }
    }

    return dsProducts;
}

Audi is id 39 and jaguar is id 38 and under Jaguar I have 220 items with the exact same Mærkeid but only 1 is being showed up 我得到的数据

It looks like you are joining on the wrong column in you query, try the following:

Select ps.Mærket AS SubCategoryName, P.DataID, P.Billed, P.Model, 
       P.Årgang, P.[Motor Type], P.Krydsmål, P.Centerhul, P.Møtrikker, 
       P.Bolter, P.Dæk, P.Fælge 
from Data.Hjuldata P  
join Data.Mærke PS 
on P.MærkeID = PS.MærkeID;

You were trying to join on P.DataID = PS.MærkeID in your original query. That didn't seem correct.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM