using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace ComboBox_C
{
public partial class ComboBox_C : ComboBox//UserControl
{
public ComboBox_C()
{
InitializeComponent();
}
#region "forClass"
bool b_AddRow = false;
object o_AddValue = "-9";//default "value"
string s_AddDisplay = "----";//default "display"
#endregion
#region "properties"
public object C_AddValue
{
set
{
o_AddValue = value;
}
get
{
return o_AddValue;
}
}
//-------------------------------------------------
public string C_AddDisplay
{
set
{
s_AddDisplay = value;
}
get
{
return s_AddDisplay;
}
}
//-------------------------------------------------
//the C_AddRow property that allows to add
//one item to the top of the items (received
//from the DataSource property of the control)
public bool C_AddRow
{
set
{
b_AddRow = value;
if (b_AddRow)
{
try
{
this.DataSource =
addEmptyRow((DataTable)this.DataSource,
o_AddValue, s_AddDisplay);
}
catch (Exception ex)
{
MessageBox.Show(
"Control:ComboBox_C//Property: C_AddRow " +
"//" + ex.Message,this.Name);
}
}
}
}
#endregion
private DataTable addEmptyRow(DataTable dtIn, object addValue,
string addDisplay)
{
DataTable dt = new DataTable();
DataColumn dCol_0 = null;
DataColumn dCol_1 = null;
DataRow dRow = null;
string sColumnName_0 = dtIn.Columns[0].Caption;
string sColumnName_1 = dtIn.Columns[1].Caption;
int iCount = 0;
bool bDoInt = true;
dCol_0 = new DataColumn();
dCol_1 = new DataColumn();
dCol_0.ColumnName = sColumnName_0;
if (addValue.GetType() == Type.GetType("System.Int32"))
{
dCol_0.DataType = Type.GetType("System.Int32");
}
else
{
dCol_0.DataType = Type.GetType("System.String");
}
dt.Columns.Add(dCol_0);
dCol_1 = new DataColumn();
dCol_1.ColumnName = sColumnName_1;
dCol_1.DataType = Type.GetType("System.String");
dt.Columns.Add(dCol_1);
dRow = dt.NewRow();
dRow[0] = addValue;
dRow[1] = addDisplay;
dt.Rows.Add(dRow);
int tempFor1 = dtIn.Rows.Count;
for (iCount = 0; iCount < tempFor1; iCount++)
{
dRow = dt.NewRow();
if (bDoInt)
{
dRow[0] = dtIn.Rows[iCount][0];
}
else
{
dRow[0] = dtIn.Rows[iCount][0].ToString();
}
dRow[1] = dtIn.Rows[iCount][1];
dt.Rows.Add(dRow);
}
return dt;
}
}
}