T
Tony Johansson
Hello!
I have a form class called WareHouse. To this class is added
a DataGridView control and some buttons and label controls.
In addition I have a class called FileManger where I handle all things that
has to do with the textfile.
Such as reading and writing.
When I click on the Save button I store the contents of the DataGridView to
a plain textfile and
when the application starts the contents of the textfile is displayed in the
DataGridView.
In the C-tor for WareHouse I do the following
public Lager()
{
InitializeComponent();
_bindingSource.DataSource = _dataTable;
dataGridViewWareHouse.DataSource = _bindingSource;
}
All variables that start with a underscore is instance members.
I also have a form load on the WareHouse which is listed below and all other
methods that is used.
In sequence I do the following in the WareHouse_Load event handler.
1. I create DataColumn in method GetNewColumn()
2. I add all created DataColumn to my DataTable.
3. I fetch all my rows from the textfile. Each row in the textfile contains
five fields.
4. I create DataRow by using the values from the textfile and add these
DataRow to my DataTable
5. At the end of the WareHouse_Load I do some important settings.
Now to my problem. A DataGridView is just a way of display the underlaying
DataTable.
So a DataGridView will just display what is defined in the DataTable
I have a column in the DataTable that is called Type which is of type string
as you can see.
What I want is that this column Type should be of type
DataGridViewComboBoxColumn so there will be
some alternatives to chose from..
Type represent different kind of media product such as book,DV,DVD and
games.
Can somebody explain how this can be done ?
private void WareHouse_Load(object sender, EventArgs e)
{
string[] rowData;
if (dataGridViewWareHouse.RowCount > 0)
_dataTable.Rows.Clear();
if (_dataTable.Columns.Count == 0)
{
_dataTable.Columns.Add(GetNewColumn("Name", "Name", "System.String"));
_dataTable.Columns.Add(GetNewColumn("Price", "Price",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("ProdNr", "ProdNr",
"System.Int32"));
_dataTable.Columns.Add(GetNewColumn("Count", "Count",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("Type", "Type", "System.String"));
}
_myList = FileManager.GetAllProducts();
foreach (string line in _myList)
{
rowData = line.Split(new char[] { ',' });
CreateNewRow(rowData);
}
DataView view = new DataView(_dataTable);
view.AllowDelete = false;
dataGridViewWareHouse.Columns["ProdNr"].ReadOnly = true;
_dataTable.AcceptChanges();
}
private DataColumn GetNewColumn(string columnName, string columnCaption,
string columnType)
{
DataColumn dataColumn = new DataColumn(columnName,
System.Type.GetType(columnType));
if (columnName == "ProdNr")
{
dataColumn.AutoIncrement = true;
dataColumn.AutoIncrementSeed = 1;
}
dataColumn.Caption = columnCaption;
return dataColumn;
}
private void CreateNewRow(string[] rowData)
{
DataRow dataRow = _dataTable.NewRow();
int i = 0;
foreach (string fieldData in rowData)
dataRow[i++] = fieldData;
_dataTable.Rows.Add(dataRow);
}
//Tony
I have a form class called WareHouse. To this class is added
a DataGridView control and some buttons and label controls.
In addition I have a class called FileManger where I handle all things that
has to do with the textfile.
Such as reading and writing.
When I click on the Save button I store the contents of the DataGridView to
a plain textfile and
when the application starts the contents of the textfile is displayed in the
DataGridView.
In the C-tor for WareHouse I do the following
public Lager()
{
InitializeComponent();
_bindingSource.DataSource = _dataTable;
dataGridViewWareHouse.DataSource = _bindingSource;
}
All variables that start with a underscore is instance members.
I also have a form load on the WareHouse which is listed below and all other
methods that is used.
In sequence I do the following in the WareHouse_Load event handler.
1. I create DataColumn in method GetNewColumn()
2. I add all created DataColumn to my DataTable.
3. I fetch all my rows from the textfile. Each row in the textfile contains
five fields.
4. I create DataRow by using the values from the textfile and add these
DataRow to my DataTable
5. At the end of the WareHouse_Load I do some important settings.
Now to my problem. A DataGridView is just a way of display the underlaying
DataTable.
So a DataGridView will just display what is defined in the DataTable
I have a column in the DataTable that is called Type which is of type string
as you can see.
What I want is that this column Type should be of type
DataGridViewComboBoxColumn so there will be
some alternatives to chose from..
Type represent different kind of media product such as book,DV,DVD and
games.
Can somebody explain how this can be done ?
private void WareHouse_Load(object sender, EventArgs e)
{
string[] rowData;
if (dataGridViewWareHouse.RowCount > 0)
_dataTable.Rows.Clear();
if (_dataTable.Columns.Count == 0)
{
_dataTable.Columns.Add(GetNewColumn("Name", "Name", "System.String"));
_dataTable.Columns.Add(GetNewColumn("Price", "Price",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("ProdNr", "ProdNr",
"System.Int32"));
_dataTable.Columns.Add(GetNewColumn("Count", "Count",
"System.String"));
_dataTable.Columns.Add(GetNewColumn("Type", "Type", "System.String"));
}
_myList = FileManager.GetAllProducts();
foreach (string line in _myList)
{
rowData = line.Split(new char[] { ',' });
CreateNewRow(rowData);
}
DataView view = new DataView(_dataTable);
view.AllowDelete = false;
dataGridViewWareHouse.Columns["ProdNr"].ReadOnly = true;
_dataTable.AcceptChanges();
}
private DataColumn GetNewColumn(string columnName, string columnCaption,
string columnType)
{
DataColumn dataColumn = new DataColumn(columnName,
System.Type.GetType(columnType));
if (columnName == "ProdNr")
{
dataColumn.AutoIncrement = true;
dataColumn.AutoIncrementSeed = 1;
}
dataColumn.Caption = columnCaption;
return dataColumn;
}
private void CreateNewRow(string[] rowData)
{
DataRow dataRow = _dataTable.NewRow();
int i = 0;
foreach (string fieldData in rowData)
dataRow[i++] = fieldData;
_dataTable.Rows.Add(dataRow);
}
//Tony