Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello ,

Can you please tell me how to add custom control to datagridview same as Gridview in asp.net .
We create a custom control because it is used for n number of times and it also content some new properties which is added by us . so can you please suggest a way to add custom control to datagridview .Following is the sample code of custom control ,we want to use same control in datagridview


C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;

namespace AddColumnsToGrid
{
    public partial class CustomBank : System.Windows.Forms.ComboBox
    {
        // string con = VriddhiERP.Properties.Settings.Default.VriddhiERPConnectionString.ToString();
        SqlConnection con = new SqlConnection(AddColumnsToGrid.Properties.Settings.Default.VriddhiERPConnectionString.ToString());

        // -------------------- AllowAddNew -------------------------------    
        #region AllowAddNew
        private bool _AllowAddNew = false;
        [Category("New Properties"),
 Description("Indicates whether allow user to add new record"),//or when user press f2 key then allow user to open the master or not
 DefaultValue(false)]
        public bool AllowAddNew
        {
            get { return _AllowAddNew; }
            set { _AllowAddNew = value; }
        }

        #endregion
        //----------------------Add All Value -----------------------------
        #region AddAllValue
        private bool _AddAllValue = false;
        [Category("New Properties"),
 Description("Indicates whether show (All) value "),//it is use in reports 
 DefaultValue(false)]
        public bool AddAllValue
        {
            get { return _AddAllValue; }
            set { _AddAllValue = value; }
        }
        # endregion
        //----------------------Add None  Value -----------------------------
        #region AddNoneValue
        private bool _AddNoneValue = false;
        [Category("New Properties"),
 Description("Indicates whether show (None) value "),
 DefaultValue(false)]
        public bool AddNoneValue
        {
            get { return _AddNoneValue; }
            set { _AddNoneValue = value; }
        }
        # endregion
        public CustomBank()
        {
            InitializeComponent();
        }

        public CustomBank(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        public string DropDownBind(string Sectionid)
        {
            string Initvalue = "0";
            con.Open();

            //create Temporary table
            SqlCommand DbCommand = new SqlCommand("CREATE TABLE dbo.#temp (BANK_ACCOUNT_ID INT,BANK_ACCOUNT varchar(50));", con);
            DbCommand.ExecuteNonQuery();

            if (AddAllValue == true)
            {
                //INSERT VALUE IN TEMPORARY TABLE
                DbCommand = new SqlCommand("INSERT INTO #temp(BANK_ACCOUNT_ID,BANK_ACCOUNT) values(-1,'--ALL--')", con);
                DbCommand.ExecuteNonQuery();


            }
            if (AddNoneValue == true)
            {
                //INSERT VALUE IN TEMPORARY TABLE
                DbCommand = new SqlCommand("INSERT INTO #temp(BANK_ACCOUNT_ID,BANK_ACCOUNT) values(0,'--NONE--')", con);
                DbCommand.ExecuteNonQuery();

            }

            //UNION ALL BOTH QUERY 
            string SelectQuery = "SELECT BANK_ACCOUNT_ID,BANK_ACCOUNT FROM SM_Bank_Account_Master WHERE SECTION_ID='" + Sectionid.ToString() + "' AND DeleteFlag='" + 0 + "' UNION ALL SELECT * FROM dbo.#temp ORDER BY BANK_ACCOUNT_ID";
            SqlDataAdapter da = new SqlDataAdapter(SelectQuery, con);
            DataTable dt = new DataTable();
            da.Fill(dt);



            if (dt.Rows.Count > 0)
            {
                this.DisplayMember = "BANK_ACCOUNT";
                this.ValueMember = "BANK_ACCOUNT_ID";
                this.DataSource = dt;
            }
            else
            {
                MessageBox.Show("Please Insert Records in Bank Master");
                Initvalue = "1";

            }
            con.Close();
            return Initvalue;
        }
    }
}
Regards

Aarti Ostwal
Posted
Updated 28-Dec-13 23:30pm
v3
Comments
Sergey Alexandrovich Kryukov 24-Dec-13 2:38am    
How this class is relevant to the question? This is not what you should do...
—SA
An@nd Rajan10 24-Dec-13 4:05am    
what type of custom control you want add? please dicribes !!
Ostwal Aarti 25-Dec-13 0:33am    
Hello
Custom Controls Consist of some properties which is not their for combobox ,
such as addnew or allow none property suppose if user select allow none the that dropdownlist contain none value and if user press f2 button then user is able to add new record to that master .so we want to use same custom control to datagridview .

This class is not what you should do. You need to develop some class(es) in the way required by DataGridView. First of all, you need to develop a specialized class of the grid view cell, the same way as other cell classes are implemented. Such class should be derived from the class System.Windows.Forms.DataGridViewCell:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell%28v=vs.110%29.aspx[^].

Additionally, you may need a special class for the column, likewise: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
Comments
Maciej Los 24-Dec-13 3:57am    
+5!
Sergey Alexandrovich Kryukov 24-Dec-13 10:52am    
Thank you, Maciej.
—SA
thatraja 24-Dec-13 10:27am    
5!
Sergey Alexandrovich Kryukov 24-Dec-13 10:53am    
Thank you, Raja.
—SA
 
Share this answer
 
Comments
thatraja 24-Dec-13 10:28am    
5!
[no name] 25-Dec-13 23:27pm    
Thanks Sir
..................
In addition of solution 1 by Sergey Alexandrovich Kryukov...

DataGridView Control Sample[^]
DataGridView Custom Column Sample[^]
 
Share this answer
 
Comments
thatraja 24-Dec-13 10:28am    
5!
Maciej Los 24-Dec-13 17:08pm    
Thank you ;)
And Happy Christmas Eve ;)

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