Wednesday, March 26, 2014

How to add custom grid view in C# ?                                                                                                      

         public void CustomGridView()
        {
            gridviewname.ColumnCount = 5;
            gridviewname.Columns[0].HeaderText = "Id";
            gridviewname.Columns[1].HeaderText = "Name";
            gridviewname.Columns[2].HeaderText = "Amount";
            gridviewname.Columns[3].HeaderText = "Expense By";
            gridviewname.Columns[4].HeaderText = "Date";
            gridviewname.Columns[0].Width = 50;
            gridviewname.Columns[1].Width = 180;
            gridviewname.Columns[2].Width = 100;
            gridviewname.Columns[3].Width = 180;
            gridviewname.Columns[4].Width = 150;
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.BackColor = Color.White;
            style.Font = new Font("Arial", 8, FontStyle.Bold);
            style.ForeColor = Color.Navy;
            style.Padding = new Padding(5, 2, 5, 5);
            style.SelectionBackColor = Color.LightBlue;
            gridviewname.DefaultCellStyle = style;

            DataGridViewCellStyle styleHdr = new DataGridViewCellStyle();
            styleHdr.Padding = new Padding(1, 1, 1, 1);
            styleHdr.BackColor = Color.OldLace;
            styleHdr.ForeColor = Color.Black;
            gridviewname.ColumnHeadersDefaultCellStyle = styleHdr;

            gridviewname.AllowUserToAddRows = false;
            gridviewname.AllowUserToOrderColumns = false;
            gridviewname.AllowUserToDeleteRows = false;
            gridviewname.AutoGenerateColumns = false;
            gridviewname.ReadOnly = false;
            gridviewname.Columns[0].ReadOnly = true;
            gridviewname.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            gridviewname.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
            gridviewname.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
            gridviewname.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
            gridviewname.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;

        }

//Call this Method.

CustomGridView();

No comments:

Post a Comment