我尝试使用此代码显示sql数据库中的一些信息。但是我看不到gridview中的数据。我怎么解决?另外,我没有错误。
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection("Server=CAN-PC; Database=SMS; UID=SA; PWD=delidana1963");
string sql = "";
sql = @"select Orginator,RecordDate, (select COUNT(TurkcellID) from SmsStore ";
// txttarih.Text = Calendar1.SelectedDate.ToString();
var tarih1 = String.Format("{0:yyyy-MM-dd hh:mm}", Calendar1.SelectedDate.Date);
var tarih2 = String.Format("{0:yyyy-MM-dd hh:mm}", Calendar2.SelectedDate.Date);
if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null)
sql += "where RecordDate between '" +tarih1 + "' and '" + tarih2 + "'";
else if (Calendar1.SelectedDate != null && Calendar2.SelectedDate == null)
sql += "where RecordDate between '" + tarih1 + "' and '" + DateTime.Now + "'";
sql += ") as toplammsj,";
sql += "(select COUNT(TurkcellID) from SmsStore where TurkcellID=1 ";
if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null)
sql += "and RecordDate between '" + tarih1 + "' and '" + tarih2+ "'";
else if (Calendar1.SelectedDate != null && Calendar2.SelectedDate == null)
sql += "and RecordDate between '" + tarih1 + "' and '" + DateTime.Now + "'";
sql += ") as giden,";
sql += " (select COUNT(TurkcellID) from SmsStore where TurkcellID=0 ";
if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null)
sql += "and RecordDate between '" + tarih1 + "' and '" + tarih2 + "'";
else if (Calendar1.SelectedDate != null && Calendar2.SelectedDate == null)
sql += "and RecordDate between '" + tarih1 + "' and '" + DateTime.Now + "'";
sql += ") as gitmeyen from SmsStore ";
if (Calendar1.SelectedDate != null && Calendar2.SelectedDate != null)
sql += "where RecordDate between '" + tarih1+ "' and '" + tarih2 + "'";
else if (Calendar1.SelectedDate != null && Calendar2.SelectedDate == null)
sql += "where RecordDate between '" + tarih1 + "' and '" + DateTime.Now + "'";
SqlDataAdapter adp = new SqlDataAdapter(sql, cnn);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.DataMember = "dt";
}
请帮我解决这个问题。 谢谢大家。
答案 0 :(得分:0)
这是WPF GridView 还是Windows Forms DataGridView? WPF GridView没有DataSource属性或DataBind()方法。对于DataGridView,如果 DataSource 是DataTable,则不需要使用DataBind()或设置DataMember属性,就像您所做的那样。
此外,出于调试目的,在行adp.Fill(dt)
之后添加断点。将光标放在dt
上并检查属性以确定Rows.Count是否大于0.如果您的SQL搞砸了,您可能不会首先从数据库中获取任何数据。