Monday, 19 February 2007

Detailed error info for datasets filling

Tyred of the "Failed to enable constraints. One
or more rows contain values violating non-null, unique, or foreign-key
constraints". " error when filling dataset?

Well, the detailed error info is buried inside the dataset (not deeply buried after all...)

But for the lazy people (like me), you can add this class to whatever project you want and just call the static method in the inmediate window, passing the dataset, to see from where the error comes from...

Example: DataSetDebugger.GetErrors(myDataset)



The class:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;

namespace DataDebug
{
class DataSetDebugger
{
public static void GetErrors(DataSet ds)
{
Debug.WriteLine("--- Checking errors on dataset: '" + ds.DataSetName + "'---");
foreach (DataTable tb in ds.Tables)
{
if (tb.HasErrors)
{
Debug.WriteLine(" Errors found on table: '" + tb.TableName + "'");
int i = 0;
foreach (DataRow dr in tb.GetErrors())
{

Debug.WriteLine(" Errors found on row: [" + i.ToString() + "]:");
Debug.WriteLine(" - Error description: " + dr.RowError);
foreach(DataColumn col in dr.GetColumnsInError())
{
Debug.WriteLine(" - Column: " + col.ColumnName + " Error: " + dr.GetColumnError(col));
}

i++;
}
}
}
Debug.WriteLine("--- Checking done ---");
Debug.WriteLine("");
}
}
}

Friday, 16 February 2007

DataGridView multiselect without CTRL

One of my mates has asked my about how to enable a datagridview multiselection without pressing CTRL key (for a touch screen). I haven't tested it throughly, but here comes a (C#) possible solution:

I´ve tested it only with a Datagridview configured for full row selection.

Basically I hold a private collection of the rows I want to select and every time a row is pressed, it´s selected or removed accordingly

The empty override method avoids selection refresh. Try to remove it and see...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;

namespace MyDataGridView
{
public partial class MyDataGridV : DataGridView
{
private Hashtable mySelectedRows = new Hashtable();

public MyDataGridV()
{

}

protected override void OnCellMouseDown(DataGridViewCellMouseEventArgs e)
{

}

protected override void OnCellClick(DataGridViewCellEventArgs e)
{
if (mySelectedRows.ContainsKey(e.RowIndex))
{
mySelectedRows.Remove(e.RowIndex);
}
else
{
mySelectedRows.Add(e.RowIndex, this.Rows[e.RowIndex]);
}
for (int i = 0; i < this.Rows.Count; i++)
{
if (mySelectedRows.Contains(i))
{
this.SetSelectedRowCore(i, true);
}
else
{
this.SetSelectedRowCore(i, false);
}
}
Debug.Assert(this.SelectedRows.Count == mySelectedRows.Count, "Selected rows number don´t match");
base.OnCellClick(e);
}


}
}