io sono bloccato con questo problema di mine e sarà di grande aiuto se qualcuno risolve questo problemaDataGridView Aggiornamento tramite un filo durante lo scorrimento
per me
Quello che sto cercando di fare è:
1) inizializzare un DataTable DataTable in carico di maschera e assegnare suo predefinita su un
datagridview dgvresult
2) su clic del ma ton iniziare un thread STA (sto effettivamente lavorando con Watin IE quindi il
necessità di rendere la STA filo) che chiama un metodo che crea un identico DataTable
dt come datatable creato nel passaggio 1 e quindi aggiunge 300 righe a questo datatable.
3) chiama un delegato che si fonde questa dt con DataTable e aggiorna quindi il dgvresult Beh qui è il mio problema:
Qui è il frammento di codice per i passi che ho appena descritto:
static class Program
{
/// <summary>
/// The main entry point for the application.This method is made STAThread as I need to intialize WatIN IE in the form load of frmMain
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
}
/// codice frmMain
/// <summary>
/// Delegate which Binds updated Datatable to gridview
/// </summary>
/// <param name="dt">The Datatable to be merged with main datatable</param>
delegate void Bind_DataTable_to_GridView_Delegate(DataTable dt);
private void bind_DataTable_to_GridView(DataTable dt)
{
if (dgvResult.InvokeRequired)
{
Bind_DataTable_to_GridView_Delegate del = new Bind_DataTable_to_GridView_Delegate(bind_DataTable_to_GridView);
dgvResult.Invoke(del, new object[] { dt });
}
else
{
datatable.Merge(dt);
dgvResult.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; //Autosizes the gridview
foreach (DataGridViewColumn dgvcol in dgvResult.Columns)
{
dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
}
WatiN.Core.IE ie;
private void frmMain_Load(object sender, EventArgs e)
{
//intialize WatIN IE
ie = new IE(URLs.mainURL);
//initialization of columns in datatable
DataColumn datacolumn1 = new DataColumn("Words");
//Adding of columns in datatable
datatable.Columns.Add(datacolumn1);
//Making the datatable permanent
datatable.AcceptChanges();
//Assigning default view of datatble as dgvResult's datasource
dgvResult.DataSource = datatable.DefaultView;
foreach (DataGridViewColumn dgvcol in dgvResult.Columns)
{
dgvcol.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
private void btnGenerateWords_Click(object sender, EventArgs e)
{
try
{
if (datatable.Rows.Count > 0)
{
//Initializes the GenerateWords Thread
GenerateWords = new Thread(GenW);
//Sets the apartment state to Static
GenerateWords.SetApartmentState(ApartmentState.STA);
//Starts the GenerateWords Thread
GenerateWords.Start();
}
}
#endregion
#region function of GenerateWords thread
/// <summary>
/// function of GenerateWords thread
/// </summary>
void GenW()
{
DataColumn datacolumn1 = new DataColumn("Words");
//Adding of columns in datatable
DataTable dt = new DataTable();
dt.Columns.Add(datacolumn1);
//At this point datatable has say 20 rows
for (int dtindex = 0; dtindex < datatable.Rows.Count; dtindex++)
{
/*Code Which adds successfully 300 fresh rows to dt*/
//sends datasource of dgvresult as dt
bind_DataTable_to_GridView(dt);
dt.Clear();
}
}
}
Ora il problema sorge quando ho molti file in datagridview (permette di assumere 1000)
Ora, se nella UI I dont fare qualsiasi cosa con datagridview questo codice funziona bene senza errori
Ma se continuo a far scorrere il datgridview quando il metodo bind si chiama l'applicazione dà l'errore e appena termina a causa della sua incapacità di aggiornare il datgridview suppongo (o forse a causa di fallimento nella invocando la datagridview)
Ecco l'errore: - Quando eseguo l'exe ottengo questo: -
eccezione non gestita si è verificato nell'applicazione.
Riferimento oggetto non impostato su un oggetto.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.DataGridViewTextBoxCell.PaintPrivate(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts, Boolean computeContentBounds, Boolean computeErrorIconBounds, Boolean paint)
at System.Windows.Forms.DataGridViewTextBoxCell.Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, Object value, Object formattedValue, String errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded) at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
ho controllato attraverso il VS, dà questo errore nel Program.cs in questa linea: - Application.Run(new frmMain())
;
Come risolvere questo problema?
Qualsiasi aiuto sarà apprezzato. Grazie :)
Quale errore state ottenendo? – svick
Hey Grazie per aver risposto. Ho modificato la domanda per includere anche l'errore –