Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear sir/Madam

i have in problem datagridview. i want to create datagridviewbutton column based on data in database that output will be like this. that means datagridview like this .



1 2 3 4 5 6 7

8 9 10 11 12 13 14


could u plz anyone help me
Posted
Updated 18-Aug-12 8:55am
v2
Comments
Santhosh Kumar Jayaraman 18-Aug-12 3:57am    
Can you be more clear?
Brinda Arumugam 18-Aug-12 4:55am    
i want datagridview output like this

1 2 3 4 5 6 7 - first row

8 9 10 11 12 13 14 - second row

each and every cell is button columns

can u plz help me
[no name] 18-Aug-12 15:54pm    
Exactly how is this any kind of a problem?

1 solution

First Create this class:
C#
public class ColumnFactory
    {
        public static DataGridViewColumn CreateColumn(DataColumn dataColumn)
        {
            DataGridViewColumn column = null;
            DataSet dataSet = dataColumn.Table.DataSet;
            DataTable parentTable = null;

            for (int i = 0; i < dataSet.Relations.Count; i++)

                if (dataSet.Relations[i].ChildColumns.ToList<datacolumn>().Contains(dataColumn))
                {
                    parentTable = dataSet.Relations[i].ChildColumns[0].Table;
                    break;
                }

            if (parentTable != null)
            {
                column = new DataGridViewComboBoxColumn()
                {
                    DataSource = parentTable,
                    // In DataSet I've add a colummn for all tables and named it
                    // Discriptor And Set Expression. For example for person or
                    // customer table expression will be 
                    // "FirstName + N' ' + LastName" or "FullName" and 
                    // for company table the expreesion will be "Title" or "Name".
                    DisplayMember = "Discriptor",
                    ValueMember = "ID"
                };
            }
            else
                column = new DataGridViewTextBoxColumn();

            column.HeaderText = column.Name = dataColumn.ColumnName;
            
            return column;
        }
    }


then create your own GridView:
C#
public class GridView: System.Windows.Forms.DataGridView
    {
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.AutoGenerateColumns = false;
        }

        public void RefreshColumns(DataTable table)
        {
            if (table == null)
                return;

            this.Columns.Clear();

            foreach (DataColumn dataColumn in table.Columns)
                 this.Columns.Add(ColumnFactory.CreateColumn(dataColumn));
        }

        protected override void OnDataError(bool displayErrorDialogIfNoHandler, DataGridViewDataErrorEventArgs e)
        {
            if (MessageBox.Show("The Entries were not valid\nDo you want to continue? ",
                "Error",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning)
                e.Cancel = true;

            displayErrorDialogIfNoHandler = false;
            base.OnDataError(displayErrorDialogIfNoHandler, e);
        }

    }


At your Form:
C#
DataGridView.RefreshColumns(DataSet.Tables[this.BindingSource.DataMember]);
 
Share this answer
 
v4
Comments
joe suresh 20-Oct-12 7:24am    
i create datagridview1 in customer(formname) and i fill the data from DB and now ineed to edit the datagridview1 from oldcustomer(formname) how can i access kindly tell me any one

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900