写了下面这个方法调用了两次,第一次是在FormLoad事件里,成功运行
第二次在按钮事件中未成功,DataGridView没填充数据,也没报错
SQL语句在数据库中运行也没问题
public static void FillMainDgv(string str,DataGridView dgv)
{
try
{
dgv.Rows.Clear();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(str, DBHelper.connection);
DataSet dataSet = new DataSet("ds");
dataAdapter.Fill(dataSet, "MainTable");
int i = 1;
foreach (DataRow row in dataSet.Tables["MainTable"].Rows)
{
int index = dgv.Rows.Add();
dgv.Rows[index].Cells["Column1"].Value = Convert.ToString(i);
dgv.Rows[index].Cells["id"].Value = row["id"];
dgv.Rows[index].Cells["date"].Value = row["dt"];
dgv.Rows[index].Cells["type"].Value = row["type"];
dgv.Rows[index].Cells["body"].Value = row["body"];
dgv.Rows[index].Cells["money"].Value = row["m"];
dgv.Rows[index].Cells["event1"].Value = row["event"];
dgv.Rows[index].Cells["remark"].Value = row["remark"];
dgv.Rows[index].Cells["time"].Value = row["t"];
i++;
}
dgv.ClearSelection();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
FormLoad的SQL语句
SELECT id, Format([date], 'yyyy/mm/dd') AS dt, type, body, format([money],'0.00') as m, event, remark, Format([time],'yyyy/mm/dd hh:nn:ss') AS t FROM MainTable WHERE DateDiff('d', [date], #2016/06/01#) <=0 AND DateDiff('d', [date], #2016/06/30#) >=0 ORDER BY [date] DESC
Button的SQL语句
SELECT id, Format([date], 'yyyy/mm/dd') AS dt, type, body, format([money],'0.00') as m, event, remark, Format([time],'yyyy/mm/dd hh:nn:ss') AS t FROM MainTable WHERE DateDiff('d', [date], #2016/06/01#) <=0 AND DateDiff('d', [date], #2016/06/30#) >=0 type='支出' AND [event] LIKE '**' OR [remark] LIKE '**' ORDER BY [date] DESC
以上语句在数据库中测试均有结果,请大家帮我看看问题出在什么地方了,谢谢了