DataGridView only displaying data if cell selected

  • Thread starter Thread starter RichT
  • Start date Start date
R

RichT

Hi all,

I am experiencing some odd behaviour with a DataGridView.

The DataGridView is bound to a DataTable, which is populated with data
from a csv file.

The column Headings appear fine, but the data only appears if I select a
cell otherwise the DGV look empty apart from the selected cell.

I am Programmatically binding the DGV to the DataTable after I have
populated the table.

if I select each individual cell then the correct data appears, but as
soon as I select another the new cell data appears and the old disappears.

I am doing a DGV.Refresh() after setting DataSource Property but this is
not helping me.

Can somebody please give me some idea what is happening here

I am using C# Express 2005 SP1 on WinXP SP2 with all latest updates

Regards
RichT
 
Ming said:
Where is your code?

chanmm

The data is definitely loaded in the data table as I have looped through
this and printed the contents.

Once the function completes it appears that the grid has only loaded one
item, as only the top leftmost cell displays data, however if I select a
different cell, then data appears in new cell but old cell appears blank

code below

any ideas please?

fileData.Data is a List of string[];


public partial class MainForm: Form
{
DataTable tableData = new DataTable();
DataRow newDataRow;
DataColumn newDataColumn;
}

private void PopulateGrid()
{
foreach ( string header in fileData.Data[ 0 ] )
{
if ( !( tableData.Columns.Contains( header ) ) )
{

DataColumn newDataCol =
tableData.Columns.Add(header, typeof(String));
newDataCol.AllowDBNull = true;
newDataCol.Unique = false;
}
else
{
DataColumn newDataCol =
tableData.Columns.Add("Duplicate_" + header, typeof(String));
newDataCol.AllowDBNull = true;
newDataCol.Unique = false;
}
}

for ( int j = 1; j < fileData.Data.Count; ++j )
{
newDataRow = tableData.NewRow();
for ( int i = 0; i < fileData.Data[ j ].Length; ++i )
{
newDataRow[ i ] = fileData.Data[ j ][ i ];
}

tableData.Rows.Add( newDataRow );
}
dataGridViewOriginal.DataSource = tableData;
dataGridViewOriginal.Refresh();
}
 
Ming said:
Where is your code?

chanmm

The data is definitely loaded in the data table as I have looped through
this and printed the contents.

Once the function completes it appears that the grid has only loaded one
item, as only the top leftmost cell displays data, however if I select a
different cell, then data appears in new cell but old cell appears blank

code below

any ideas please?

fileData.Data is a List of string[];


public partial class MainForm: Form
{
DataTable tableData = new DataTable();
DataRow newDataRow;
DataColumn newDataColumn;
}

private void PopulateGrid()
{
foreach ( string header in fileData.Data[ 0 ] )
{
if ( !( tableData.Columns.Contains( header ) ) )
{

DataColumn newDataCol = tableData.Columns.Add(header,
typeof(String));
newDataCol.AllowDBNull = true;
newDataCol.Unique = false;
}
else
{
DataColumn newDataCol = tableData.Columns.Add("Duplicate_"
+ header, typeof(String));
newDataCol.AllowDBNull = true;
newDataCol.Unique = false;
}
}

for ( int j = 1; j < fileData.Data.Count; ++j )
{
newDataRow = tableData.NewRow();
for ( int i = 0; i < fileData.Data[ j ].Length; ++i )
{
newDataRow[ i ] = fileData.Data[ j ][ i ];
}

tableData.Rows.Add( newDataRow );
}
dataGridViewOriginal.DataSource = tableData;
dataGridViewOriginal.Refresh();
}
 
Back
Top