Yaz  Font K   lt Yaz  Font B y lt

DataGridView Nesnesinde Seçili Satırın Sınır Çizgisini ve Arka Plan Rengini Değiştirmek 

 

Merhaba arkadaşlar bu makalemizde dataGridView nesnesinde seçeceğimiz satırın kenar çizgisini ve seçili satırın arka plan rengini değiştireceğiz. Bunu yapabilmemiz için dataGridView nesnesinin RowPostPaint olayına aşağıdaki kodları yazın.

 

 

Resim1

Şekil 1

 

Form1.cs

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace make_border_in_row_datagridview

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            bindData();

        }

 

        void bindData()

        {

            SqlConnection con = new SqlConnection("Data Source=Sirius;Initial Catalog=db;User ID=sa;Password=2344*;Integrated Security=true");

 

            con.Open();

 

            SqlCommand cmd = new SqlCommand("Select * From dbo.employees ", con);

 

            SqlDataAdapter da = new SqlDataAdapter(cmd);

 

 

 

            DataTable dt = new DataTable();

 

            da.Fill(dt);

 

            con.Close();

 

            dataGridView1.DataSource = dt;

        }

 

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)

        {

            if (e.RowIndex == 5)

            {

                // Calculate the bounds of the row

                // Satir sinirlarini hesapliyoruz

                int rowHeaderWidth = dataGridView1.RowHeadersVisible ?

                dataGridView1.RowHeadersWidth : 0;

                Rectangle rowBounds = new Rectangle(

                  rowHeaderWidth,

                  e.RowBounds.Top,

                   dataGridView1.Columns.GetColumnsWidth(

                      DataGridViewElementStates.Visible) -

                       dataGridView1.HorizontalScrollingOffset + 1,

                 e.RowBounds.Height);

                // Paint the border

                // Sinir çizgisini boyuyoruz. Bu ornekte sinir

                //cizgisini kirmizi yapiyoruz

                ControlPaint.DrawBorder(e.Graphics, rowBounds,

                        Color.Red, ButtonBorderStyle.Solid);

 

                // Paint the background color

                // Secili satirin arka plan rengini acik mavi

                // yapiyoruz.

                 dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor =

                                  Color.LightBlue;

            }

        }

    }

}

     

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN