I have two combobox (combobox1 in form 1 and combobox2 in form2). I need to import an excel file and the combobox1 gets the sheets name. Then in the form2 (I don't want to import the same file another time) i have combobox2 that have also the same sheets name from the combobox1.
I tried this but i got the error of NULLFUNCTION Expression(Additional Information: The object reference is not set to an instance of an object).
using (OpenFileDialog openFileDialog = new OpenFileDialog() { Filter = "Excel 97-2003 Workbook|*.xls|Excel Workbook|*.xlsx |fichiers Textes (*.txt)|*.txt|fichiers CSV (*.csv)|*.csv|tous les fichiers (*.*)|*.*" })
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
TextBox_Emplacement.Text = openFileDialog.FileName;
using (var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
{
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true }
});
dataTableCollection = result.Tables;
dataTableCollection1 = result.Tables;
sheet.Items.Clear();
//cp.radDropDownList1.Items.Clear();
foreach (DataTable table in dataTableCollection)
sheet.Items.Add(table.TableName);//add sheet to combobox
**Form_Copie cp1 = (Form_Copie)Application.OpenForms["Form_Copie"];
foreach (DataTable table in dataTableCollection)
cp1.radDropDownList1.Items.Add(table.TableName);//add sheet to combobox**
If the item you're trying to pass is just the combobox string value Could pass the value as you load the new form
{
string sheet = comboBox1.Text;
Form frm2 = new Form2(sheet);
frm2.show();
}
Public Form2(string sheet)
{
Initialize();
comboBox2.Text = sheet;
}
There are a few ways this can be done. Overloading the constructor is a good way, but doing this per control can quickly become daunting and messy. If you want to do this for multiple controls I'd suggest using a model that contains properties of Bindings (eg System.ComponentModel.BindingList and/or System.Forms.Binding) as a field in each form. Then overload the constructor and pass in the bindings through the model class.
public class BindingModel {
public property BindingList<string> ComboBoxBinding {get;set;}
... other binding/non-binding properties ...
}
public class Form1{
private BindingModel _bindingModel = new _bindingModel();
private void Form1_Shown(object sender, EventArgs e) {
this.ComboBox1.DataSource = _bindingModel.ComboBoxBinding;
this.ComboBox1.DataMember = "";
// the add would go in your excel method
_bindingModel.ComboBoxBinding.Add("Item 1");
}
private void btnOpenForm2_Click(object sender, EventArgs e) {
var frm = new Form2(_bindingModel);
frm.Show();
this.Close();
}
}
public class Form2{
private BindingModel _bindingModel = new _bindingModel();
public void Form2(BindingModel bindingModel){
this._bindingModel = bindingModel;
}
// Once the form is shown bind the ComboBox and it will populate the values
private void Form2_Shown(object sender, EventArgs e){
this.ComboBox1.DataSource = _bindingModel.ComboBoxBinding;
this.ComboBox1.DataMember = "";
}
}
Alternatively it is possible to set the controls items within the calling form:
public class Form1{
private void btnOpenForm2_Click(object sender, EventArgs e) {
var frm = new Form2();
for(int i = 0; i < this.ComboBox1.Count - 1; i++){
frm.ComboBox1.Items.Add(ComboBox1.Items[i]);
}
frm.Show();
this.Close();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.