组合框2中的日期每当我更改组合框1时总是发生更改1
问题描述:
我的程序中有2个组合框(组合框1和组合框2),我遇到问题,当我选择日期“10/10/2014” ComboBox 1,ComboBox 2的改变与我在ComboBox 1中完全相同,为什么它是这样的?组合框2中的日期每当我更改组合框1时总是发生更改1
下面是代码:
string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\db1.accdb";
private const int CP_NOCLOSE_BUTTON = 0x200;
private Choices _choice;
private DataSet _ds = new DataSet();
private List<DateTime> _dateTime = new List<DateTime>();
public Trans()
{
InitializeComponent();
}
public Trans(Choices _choice)
: this()
{
this._choice = _choice;
}
private void Trans_Load(object sender, EventArgs e)
{
for (int i = 0; i < DateTime.Today.AddYears(1).Subtract(DateTime.Today).TotalDays + 1; i++)
{
_dateTime.Add(DateTime.Today.AddDays(i));
}
ViewDatabase(sender, e);
StartDateCollection(sender, e);
EndDateCollection(sender, e);
this.dataGridView1.Columns["ID"].Visible = false;
this.dataGridView1.Sort(this.dataGridView1.Columns["Times"], System.ComponentModel.ListSortDirection.Ascending);
this.label3.Text = "Welcome, " + UserInformation.CurrentLoggedInUser + " " + " " + "-" + " " + " " + UserInformation.CurrentLoggedInUserType;
this.label3.ForeColor = System.Drawing.Color.White;
dataGridView1.RowPostPaint += new DataGridViewRowPostPaintEventHandler(this.SetRowNumber);
dataGridView1.ClearSelection();
}
private void ViewDatabase(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "SELECT [ProductCode], [Quantity], [Description], [SubTotal], [Total], [IssuedBy], [To], [Dates], [Times] FROM [TransRecord]";
conn.Open();
using (OleDbDataAdapter _adapter = new OleDbDataAdapter(query, conn))
{
_ds.Clear();
_adapter.Fill(_ds, "TransRecord");
dataGridView1.DataSource = null;
dataGridView1.Refresh();
}
dataGridView1.DataSource = _ds.Tables[0];
conn.Close();
}
}
private void SetRowNumber(object sender, DataGridViewRowPostPaintEventArgs e)
{
var grid = sender as DataGridView;
var rowIdx = (e.RowIndex + 1).ToString();
var centerFormat = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
var headerBounds = new Rectangle(e.RowBounds.Left, e.RowBounds.Top, grid.RowHeadersWidth, e.RowBounds.Height);
e.Graphics.DrawString(rowIdx, this.Font, SystemBrushes.ControlText, headerBounds, centerFormat);
}
private void StartDateCollection(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "SELECT [Dates] FROM [TransRecord]";
conn.Open();
using (OleDbDataAdapter _adapter = new OleDbDataAdapter(query, conn))
{
comboBox1.DataSource = _dateTime;
comboBox1.FormatString = "M/dd/yyyy";
comboBox1.FormattingEnabled = true;
}
}
}
private void EndDateCollection(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "SELECT [Dates] FROM [TransRecord]";
conn.Open();
using (OleDbDataAdapter _adapter = new OleDbDataAdapter(query, conn))
{
comboBox2.DataSource = _dateTime;
comboBox2.FormatString = "M/dd/yyyy";
comboBox2.FormattingEnabled = true;
}
}
}
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
QuitProgram(sender, e);
}
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintFile(sender, e);
}
private void button1_Click(object sender, EventArgs e)
{
GetData(sender, e);
}
private void button2_Click(object sender, EventArgs e)
{
Clear(sender, e);
}
private void PrintFile(object sender, EventArgs e)
{
}
private void GetData(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "SELECT [ProductCode], [Quantity], [Description], [SubTotal], [Total], [IssuedBy], [To], [Dates], [Times] FROM [TransRecord] WHERE [Dates] = @Dates ORDER BY [Dates]";
conn.Open();
using (OleDbCommand cmd = new OleDbCommand(query, conn))
{
cmd.Parameters.Add("@Dates", System.Data.OleDb.OleDbType.Date);
cmd.Parameters["@Dates"].Value = this.comboBox1.SelectedValue;
using (OleDbDataAdapter _adapter = new OleDbDataAdapter(cmd))
{
_ds.Clear();
_adapter.Fill(_ds, "TransRecord");
dataGridView1.DataSource = null;
dataGridView1.Refresh();
}
dataGridView1.DataSource = _ds.Tables[0];
conn.Close();
}
}
}
private void QuitProgram(object sender, EventArgs e)
{
if (_choice.comboBox1.Text == "English")
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("Are You Sure Want to Quit?", "Warning", MessageBoxButtons.YesNo);
if (_dialogResult == DialogResult.Yes)
{
this.Hide();
this.Close();
}
else
{
}
}
else if (_choice.comboBox1.Text == "Indonesian")
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("Apakah Kamu Benar-benar mau Keluar?", "Warning", MessageBoxButtons.YesNo);
if (_dialogResult == DialogResult.Yes)
{
this.Hide();
this.Close();
}
else
{
}
}
}
private void Clear(object sender, EventArgs e)
{
if (_choice.comboBox1.Text == "English")
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("Are You Sure Want to Clear all the Data?", "Warning", MessageBoxButtons.YesNo);
if (_dialogResult == DialogResult.Yes)
{
ClearDatabase(sender, e);
}
else
{
}
}
else if (_choice.comboBox1.Text == "Indonesian")
{
System.Media.SoundPlayer _sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sound.Play();
DialogResult _dialogResult = MessageBox.Show("Apakah Kamu Yakin mau Menghapus semua Data?", "Warning", MessageBoxButtons.YesNo);
if (_dialogResult == DialogResult.Yes)
{
ClearDatabase(sender, e);
}
else
{
}
}
}
private void ClearDatabase(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
string query = "DELETE FROM [TransRecord]";
conn.Open();
using (OleDbDataAdapter _adapter = new OleDbDataAdapter(query, conn))
{
_ds.Clear();
_adapter.Fill(_ds, "TransRecord");
dataGridView1.DataSource = null;
dataGridView1.Refresh();
}
dataGridView1.DataSource = _ds.Tables[0];
conn.Close();
}
if (_choice.comboBox1.Text == "English")
{
System.Media.SoundPlayer _sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sounds.Play();
MessageBox.Show("Cleared!", "Cleared");
}
else if (_choice.comboBox1.Text == "Indonesian")
{
System.Media.SoundPlayer _sounds = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Exclamation.wav");
_sounds.Play();
MessageBox.Show("Berhasil Dibersihkan!", "Cleared");
}
}
protected override CreateParams CreateParams
{
get
{
CreateParams myCp = base.CreateParams;
myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
return myCp;
}
}
}
这里是截图:
在上图中,当窗体负载,在组合框的日期1是2013年9月22日。
在上面的图片,我只是想在组合框中1日起更改为2013年9月24日,但它改变组合框2日了。
如何解决?由于
答
这是因为你使用相同的DataSource
_datetime
两个comboboxes
:
comboBox1.DataSource = _dateTime;
comboBox2.DataSource = _dateTime.ToList();
但是我认为_dateTime
可能不是你想要的设置为DateSource
两者的组合框,如果是这样的话,见好就收一些评论。
+0
谢谢!它现在解决了! – Kaoru
您的两个组合框具有相同的数据源。你必须分离它们的Bindingcontextes。 –
谢谢!现在解决! – Kaoru
当您复制粘贴控件时发生。 – Rohit