私は問題を抱えています。コンボボックスにアイテムを追加するにはどうすればよいですか?
私はすでにこのコードを試しました:
comboBox1.Items.Add("--Dates--");
comboBox1.SelectedIndex = 0;
しかし、プログラムを実行すると、コンボボックスにアイテムを追加できません。
コードは次のとおりです。
public partial class Trans : Form
{
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> _startDate = new List<DateTime>();
private List<DateTime> _endDate = new List<DateTime>();
int startDate;
int endDate;
public Trans()
{
InitializeComponent();
}
public Trans(Choices _choice)
: this()
{
this._choice = _choice;
}
private void Trans_Load(object sender, EventArgs e)
{
startDate = (int)DateTime.Today.AddYears(5).Subtract(DateTime.Today).TotalDays + 1;
endDate = (int)DateTime.Today.AddYears(5).Subtract(DateTime.Today).TotalDays + 1;
for (int i = 0; i < startDate; i++)
{
_startDate.Add(DateTime.Today.AddDays(i));
}
for (int i = 0; i < endDate; i++)
{
_endDate.Add(DateTime.Today.AddDays(i));
}
StartDateCollection(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)
{
comboBox1.DataSource = _startDate;
comboBox1.FormatString = "M/dd/yyyy";
comboBox1.FormattingEnabled = true;
}
private void StartDateCollection_SelectedIndexChanged(object sender, EventArgs e)
{
DateTime comboBox1_SelectedDate = Convert.ToDateTime(comboBox1.SelectedValue);
List<DateTime> tempDate = _endDate.Where(d => d > comboBox1_SelectedDate).ToList<DateTime>();
comboBox2.DataSource = tempDate;
comboBox2.FormatString = "M/dd/yyyy";
comboBox2.FormattingEnabled = true;
}
(....その他のコード)
Items.Add を StartDateCollection 関数に入れましたが、追加した文字列は表示されず、日付のみが表示されます。
どうすれば修正できますか?
ありがとう。
または、初日からの取引記録が欲しいです。
例:この日の取引記録が24 September 2013
ありますが、明日プログラムを開く24 September 2013
と、日付24 September 2013
が消えてしまい、 の取引記録を開くことができません。取引実績の初日からコンボボックスの項目が表示されるようにしたい。
ご回答ありがとうございます どうもありがとうございました!
スクリーンショットは次のとおりです。
上の図からわかるように、ComboBox の開始日はです。表示される前にDate
を作成したいと思います。これは次のようになります (comboBox 内) ---Dates--- 9/24/2013 9/25/2013 ...... (など)---Dates---
Date