[英]c# linqtosql datagridview completely empty
我有一个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;
将“自动生成的列”属性设置为true可能会解决您的问题,在绑定数据源之前将其设置为
dgvIkinciEl.AutoGenerateColumns = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.