【问题标题】:c# linqtosql datagridview completely emptyc# linqtosql datagridview 完全为空
【发布时间】:2016-10-28 08:22:17
【问题描述】:

我有一个 datagridview,我想用 linq 查询加载它的数据。当我调试时,我可以看到数据源具有正确的数据和查询,但它们没有显示在 dgv 中,它完全是空的,没有行没有列。我检查了许多已经提出的问题,但无法解决。顺便说一句,它是一个 Windows 窗体应用程序。我还有另一个 linq 查询可以插入一些数据并且工作正常,所以我认为问题不在于 linqtosql 类。

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;

【问题讨论】:

    标签: c# linq datagridview


    【解决方案1】:

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

    dgvIkinciEl.AutoGenerateColumns = true;

    【讨论】:

      猜你喜欢
      • 2014-08-24
      • 2010-11-01
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多