C#-DataGridView实现隔行变色

本文介绍了一个使用C#实现DataGridView控件隔行换色的方法。通过遍历DataGridView的行并根据行索引的奇偶性改变背景色,实现了数据表格的美化效果。代码示例展示了如何在加载数据后自动应用这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AlternationColor
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }

        private void Frm_Main_Load(object sender, EventArgs e)
        {
            dgv_Message.DataSource = new List<Fruit>() {//绑定数据集合
            new Fruit(){Name="苹果",Price=30},
            new Fruit(){Name="橘子",Price=40},
            new Fruit(){Name="鸭梨",Price=33},
            new Fruit(){Name="水蜜桃",Price=31}};
            dgv_Message.Columns[0].Width = 200;//设置列宽度
            dgv_Message.Columns[1].Width = 170;//设置列宽度
            dgv_Message.SelectionMode =//设置如何选中单元格
                DataGridViewSelectionMode.FullRowSelect;
        }

        private void btn_Begin_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgv_Message.Rows.Count; i++)
            {

                if (i % 2 == 0)
                    dgv_Message.Rows[i].DefaultCellStyle.
                        BackColor = Color.LightYellow;//隔行更换背景色
            }
        }
    }
}
 

以下是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、付费专栏及课程。

余额充值