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
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
{
SqlConnection con = new SqlConnection(AddColumnsToGrid.Properties.Settings.Default.VriddhiERPConnectionString.ToString());
#region AllowAddNew
private bool _AllowAddNew = false;
[Category("New Properties"),
Description("Indicates whether allow user to add new record"),
DefaultValue(false)]
public bool AllowAddNew
{
get { return _AllowAddNew; }
set { _AllowAddNew = value; }
}
#endregion
#region AddAllValue
private bool _AddAllValue = false;
[Category("New Properties"),
Description("Indicates whether show (All) value "),
DefaultValue(false)]
public bool AddAllValue
{
get { return _AddAllValue; }
set { _AddAllValue = value; }
}
# endregion
#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();
SqlCommand DbCommand = new SqlCommand("CREATE TABLE dbo.#temp (BANK_ACCOUNT_ID INT,BANK_ACCOUNT varchar(50));", con);
DbCommand.ExecuteNonQuery();
if (AddAllValue == true)
{
DbCommand = new SqlCommand("INSERT INTO #temp(BANK_ACCOUNT_ID,BANK_ACCOUNT) values(-1,'--ALL--')", con);
DbCommand.ExecuteNonQuery();
}
if (AddNoneValue == true)
{
DbCommand = new SqlCommand("INSERT INTO #temp(BANK_ACCOUNT_ID,BANK_ACCOUNT) values(0,'--NONE--')", con);
DbCommand.ExecuteNonQuery();
}
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