请教个问题,我想针对datagridview所查出来行数,求个合计,合计数显示在下面文本框--合计数量里面,我所写得语句如下 ,运行有报错,敬请指导,谢谢;
if (this.comboBox1.SelectedIndex == 0)//comboBox1下拉框选择笫一项
{
conn = new SqlConnection("server=.;database=focus_wohua;user=sa;password=whyy"); ;
SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.cx_cxinputm WHERE cyear like '%" + textBox2.Text + "%' AND cmonth like '%" + textBox3.Text + "%' AND ISNULL(biszf,'') != '是' ", conn);
//数据适配器
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
//DataTable存储数据
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
int count = dataGridView1.RowCount;
int text = 0;
for (int i = 0; i < count; i++)
{
int value = Convert.ToInt32( dt.Rows[0]["Value"]); ;
if (!string.IsNull Or Empty(value))
{
text += Convert.ToInt32(value);
}
}
textBox1.Text = text.ToString();
}
你这里的空格那里来的?
不知道你这个问题是否已经解决, 如果还没有解决的话:由于在使用SQL查询大量的数据并一次显示到dataGridView控件,出现拖拉的时候卡顿。
解决方法:
1.首先分页。
2.其次把显示控件设置双buffer。
解决过程如下:
1.设置dataGridView双buffer代码如下,需要引用反射命名空间
Type dgvType = this.dataGridView1.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(this.dataGridView1, true, null);
2.自己定义分页操作控件,上一页,下一页,首页,尾页,在加载form的时候添加事件。并为他们写一个操作事件
private void Form1_Load(object sender, EventArgs e)
{
this.btn_EndPage.Click += Ctrl_Click;
this.btn_FirstPage.Click += Ctrl_Click;
this.btn_LastPage.Click += Ctrl_Click;
this.btn_NextPage.Click += Ctrl_Click;
//this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
//this.UpdateStyles();
}
private void Ctrl_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.Text == this.btn_EndPage.Text)
{
if (currentPage == pageCount)
{ return; }
currentPage = pageCount;
LoadPage();
}
else if (btn.Text == this.btn_FirstPage.Text)
{
if (currentPage == 1)
{ return; }
currentPage = 1;
LoadPage();
}
else if (btn.Text == this.btn_LastPage.Text)
{
if (currentPage == 1)
{ return; }
currentPage--;
LoadPage();
}
else if (btn.Text == this.btn_NextPage.Text)
{
if (currentPage == pageCount)
{ return; }
currentPage++;
LoadPage();
}
else
{
MessageBox.Show("error");
}
}
3.下面代码是分页方法,加载page
#region datagridview sort
/// <summary>
/// 每页记录数
/// </summary>
private int pageSize = 50;
/// <summary>
/// 总记录数
/// </summary>
private int recordCount = 0;
/// <summary>
/// 总页数
/// </summary>
private int pageCount = 0;
/// <summary>
/// 当前页
/// </summary>
private int currentPage = 0;
/// <summary>
/// 分页的方法
/// </summary>
/// <param name="str"></param>
private void PageSorter()
{
foreach (DataColumn col in dt.Columns)
{
DataGridViewTextBoxColumn dgvc = new DataGridViewTextBoxColumn();
dgvc.Name = "";
dgvc.DataPropertyName = "";
dgvc.HeaderText = col.ToString();
dataGridView1.Columns.Add(dgvc);
}
recordCount = dt.Rows.Count; //记录总行数
pageCount = (recordCount / pageSize);
if ((recordCount % pageSize) > 0)
{
pageCount++;
}
//默认第一页
currentPage = 1;
LoadPage();//调用加载数据的方法
}
/// <summary>
/// LoadPage方法
/// </summary>
private void LoadPage()
{
if (currentPage < 1) currentPage = 1;
if (currentPage > pageCount) currentPage = pageCount;
int beginRecord; //开始指针
int endRecord; //结束指针
DataTable dtTemp;
dtTemp = dt.Clone();
beginRecord = pageSize * (currentPage - 1);
if (currentPage == 1) beginRecord = 0;
endRecord = pageSize * currentPage;
if (currentPage == pageCount) endRecord = recordCount;
for (int i = beginRecord; i < endRecord; i++)
{
dtTemp.ImportRow(dt.Rows[i]);
}
dataGridView1.Rows.Clear();
this.lb_CurrentRow.Text = "当前页: " + currentPage.ToString() + " / " + pageCount.ToString();//当前页
this.lb_TotalRows.Text = "总行数: " + recordCount.ToString() + " 行";//总记录数
把临时table的数据插入到datagridview控件里面。
for (int i = 0; i < dtTemp.Rows.Count; i++)
{
dataGridView1.Rows.Add();
for (int j = 0; j < dtTemp.Columns.Count; j++)
dataGridView1.Rows[i].Cells[j].Value = dtTemp.Rows[i][j].ToString();
}
}
#endregion