简体   繁体   中英

c# linqtosql datagridview completely empty

I have a datagridview that I wanna load its data with a linq query. When I debug I can see datasource has the right data and query but they doesnt show up in the dgv, its completely empty, no rows no colums. I checked many already asked questions about that but couldn't solve it. Its a windows forms app btw. I also have another linq query that inserts some data and it works fine, so I assume the problem is not about the linqtosql classes.

namespace StokUygulamasi

[Database(Name = "HarleyDavidson")]

class HarleyDavidson : DataContext
{
    public HarleyDavidson() : base("Data Source=SELCUK-CODE\\LOCALHOST;Initial Catalog = HarleyDavidson; Integrated Security = true")
    {

    }
    public HarleyDavidson(SqlConnection con) : base(con) { }

    public Table<Motor> Motor
    {
        get
        {
            return this.GetTable<Motor>();
        }
    }

    public Table<IkinciEl> IkinciEl
    {
        get
        {
            return this.GetTable<IkinciEl>();
        }
    }

    public Table<Musteri> Musteri
    {
        get
        {
            return this.GetTable<Musteri>();
        }
    }

    public Table<Satis> Satis
    {
        get
        {
            return this.GetTable<Satis>();
        }
    }
}

[Table(Name = "Motor")]
public class Motor
{
    [Column(Storage = "MotorID", DbType ="Int Not null", IsPrimaryKey = true, IsDbGenerated = true)]
    public int MotorID;
    [Column(Storage = "Marka", DbType = "nvarchar(20) Not null")]
    public string Marka;
    [Column(Storage = "Model", DbType = "nvarchar(20) Not null")]
    public string Model;
    [Column(Storage = "Seri", DbType = "nvarchar(20) Not null")]
    public string Seri;
    [Column(Storage = "Plaka", DbType = "nvarchar(20) Not null")]
    public string Plaka;
    [Column(Storage = "Sasi", DbType = "char(17) Not null")]
    public string Sasi;
    [Column(Storage = "KM", DbType = "int Not null")]
    public int KM;
    [Column(Storage = "Yil", DbType = "int Not null")]
    public int Yil;
    [Column(Storage = "AlisFiyat", DbType = "decimal(18,2) Not null")]
    public decimal AlisFiyat;
    [Column(Storage = "SatisFiyat", DbType = "decimal(18,2) Not null")]
    public decimal SatisFiyat;
}
[Table(Name = "Musteri")]
public class Musteri
{
    [Column(Storage = "MusteriID", DbType = "Int Not null", IsPrimaryKey = true, IsDbGenerated = true)]
    public int MusteriID;
    [Column(Name = "Ad", DbType = "nvarchar(100) Not null")]
    public string Ad;
}

[Table(Name = "Satis")]
public class Satis
{
    [Column(Name = "SatisID", DbType = "Int Not null", IsPrimaryKey = true, IsDbGenerated = true)]
    public int SatisID;
    [Column(Name = "Tarih", DbType = "Date Not null")]
    public DateTime Tarih;
    [Column(Name = "Musteri", DbType = "nvarchar(100) Not null")]
    public string Musteri;
    [Column(Name = "Arac", DbType = "nvarchar(10) Not null")]
    public string Arac;
    [Column(Name = "AracSasi", DbType = "char(17) Not null")]
    public string AracSasi;
    [Column(Name = "Tutar", DbType = "decimal(18,2) Not null")]
    public decimal Tutar;



}

[Table(Name = "IkinciEl")]
public class IkinciEl
{
    [Column(Name = "IkinciElID", DbType = "int Not null", IsPrimaryKey = true, IsDbGenerated = true)]
    public int IkinciElID;
    [Column(Name = "Marka", DbType = "nvarchar(20) Not null")]
    public string Marka;
    [Column(Name = "Model", DbType = "nvarchar(20) Not null")]
    public string Model;
    [Column(Name = "Seri", DbType = "nvarchar(20) Not null")]
    public string Seri;
    [Column(Name = "Plaka", DbType = "nvarchar(10) Not null")]
    public string Plaka;
    [Column(Name = "Sasi", DbType = "char(17) Not null")]
    public string Sasi;
    [Column(Name = "KM", DbType = "int Not null")]
    public int KM;
    [Column(Name = "Yil", DbType = "int Not null")]
    public int Yil;
    [Column(Name = "Fiyat", DbType = "decimal(18,2) Not null")]
    public decimal Fiyat;
}

private void Form1_Load(object sender, EventArgs e)
    {

        hdc = new HarleyDavidson(con);
        var sonuc = from a in hdc.IkinciEl select a;

        dgvIkinciEl.DataSource = sonuc.ToList();

        //var bindingSource = new BindingSource();
        //bindingSource.DataSource = sonuc.ToList();
        //dgvIkinciEl.DataSource = bindingSource;
    }

this.dgvIkinciEl.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dgvIkinciEl.Location = new System.Drawing.Point(3, 3);
        this.dgvIkinciEl.Name = "dgvIkinciEl";
        this.dgvIkinciEl.Size = new System.Drawing.Size(648, 247);
        this.dgvIkinciEl.TabIndex = 0;

将“自动生成的列”属性设置为true可能会解决您的问题,在绑定数据源之前将其设置为

dgvIkinciEl.AutoGenerateColumns = true;

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