C#_串口调试助手-DataGridView卡死、假死

C#的DataGridView表格显示控件,非常容易导致软件卡死,

如果一直快速显示数据,前8-9条都还好,一旦行数特别大,直接导致整个软件卡死。

为了解决此问题需要重绘一下数据。使用以下方法解决,并且增加try catch也是必须的。

需要显示数据调用 ShowData_To_DataGridView 进行显示。

但是如果增加的行数特别多,仍不会实时显示,会一直增加行数,直到显示完毕所有指定的行数,期间会卡顿,但不会造成软件崩溃

private delegate void RetryShowData(string time, string data);

public void ShowData_To_DataGridView(string time, string data)
{
    try
    {
        //*       如果显示出错
        if (DataGridView_ShowReceiveData.InvokeRequired) //*显示出错,重绘数据到窗口
        {
            RetryShowData c = new RetryShowData(ShowData_To_DataGridView);
            this.Invoke(c, new object[] { time, data });
        }
        else
        {
            int index = this.DataGridView_ShowReceiveData.Rows.Add();
            this.DataGridView_ShowReceiveData.Rows[index].Cells[0].Value = time;
            this.DataGridView_ShowReceiveData.Rows[index].Cells[1].Value = data;
            this.DataGridView_ShowReceiveData.FirstDisplayedScrollingRowIndex = this.DataGridView_ShowReceiveData.Rows.Count - 1;
        }
    }
    catch { }
}

 个人见解,感谢阅读。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C#中使用存储过程实现DataGridView分页功能的示例: 1. 创建存储过程 首先,在数据库中创建一个存储过程来实现分页功能。下面是一个示例存储过程: ``` CREATE PROCEDURE [dbo].[sp_GetCustomersPaged] @PageNumber INT, @PageSize INT AS BEGIN SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY CustomerID) AS RowNum, * FROM Customers ) AS RowConstrainedResult WHERE RowNum >= (@PageNumber - 1) * @PageSize + 1 AND RowNum <= @PageNumber * @PageSize ORDER BY RowNum END ``` 该存储过程的作用是返回指定页码和每页显示行数的数据。 2. 在C#中调用存储过程 接下来,在C#代码中使用存储过程来实现DataGridView分页功能。首先,需要在窗体中添加一个DataGridView控件,并在代码中添加以下代码: ``` private void BindData(int pageNumber, int pageSize) { string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=True"; using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // 创建一个SQL命令对象,调用存储过程 SqlCommand command = new SqlCommand("sp_GetCustomersPaged", connection); command.CommandType = CommandType.StoredProcedure; // 添加存储过程的参数 command.Parameters.AddWithValue("@PageNumber", pageNumber); command.Parameters.AddWithValue("@PageSize", pageSize); // 创建一个DataAdapter对象,用于填充DataSet SqlDataAdapter adapter = new SqlDataAdapter(command); // 创建一个DataSet对象,并填充数据 DataSet dataSet = new DataSet(); adapter.Fill(dataSet); // 将DataSet的数据绑定到DataGridView控件上 dataGridView1.DataSource = dataSet.Tables[0]; connection.Close(); } } ``` 在上面的代码中,我们首先创建了一个SqlConnection对象,用于连接数据库。然后,创建一个SqlCommand对象,设置其CommandType为StoredProcedure,并添加存储过程的参数。接下来,创建一个SqlDataAdapter对象,用于填充DataSet。最后,将DataSet的数据绑定到DataGridView控件上。 3. 实现分页按钮 最后,我们需要实现分页按钮,以便用户可以在DataGridView中浏览不同的页码。在窗体中添加两个按钮,一个用于向前翻页,一个用于向后翻页,并在代码中添加以下代码: ``` private int currentPageNumber = 1; private int pageSize = 10; private void btnPreviousPage_Click(object sender, EventArgs e) { if (currentPageNumber > 1) { currentPageNumber--; BindData(currentPageNumber, pageSize); } } private void btnNextPage_Click(object sender, EventArgs e) { currentPageNumber++; BindData(currentPageNumber, pageSize); } ``` 在上面的代码中,我们使用currentPageNumber变量来跟踪当前页码,并使用pageSize变量来指定每页显示的行数。然后,在btnPreviousPage_Click和btnNextPage_Click事件中,我们只需递增或递减currentPageNumber变量,并调用BindData方法来重新绑定DataGridView的数据即可。 这样,我们就完成了使用存储过程来实现DataGridView分页功能的C#代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值