Tuesday, September 21, 2010

CurrencyManager exception when repopulating datatable bound to a datagridview

Another gotcha..

I have a datagridview which is bound to a bindinglist which inturn is pointed to a datatable. Everything works fine until I clear the datatable and repopoulate the table. Then I start seeing the IndexOutOfRange and CurrencyManager exceptions. The gridview also tries to add more rows than what are in the datatable - wierd!!

Every time I had repopulated the datatable I was rebinding my bindinglist agian to the datatable - turns out this was the culprit - although I haven't figured out why!! So I got rid of these error by pointing the bindingsource to the datatable only once - on the OnLoad() event.


private void DataGrid_Load(object sender, EventArgs e)
{
bindingSource1.DataSource = dataset;
bindingSource1.DataMember = "tablename";
}
//and when I need to refresh the grid:
bindingSource1.ResetBindings(false);
ResetBindings just refreshes the binding source and reads the new values form the datatable. The lone parameter will be true only if the schema of the source has changed, in my case it hasn't

Capturing the value of a databound DataGridViewCheckBoxCell

I had a DataGridViewCheckBoxColumn in my DataGridView that was bound to a BindingSource. I wanted to capture the value of the CheckBoxCell when a user checked or unchecked the cell. So I set the TrueValue and FalseValue for the cell and used the

satelliteScenesDataGridView["inCartDataGridViewCheckBoxColumn", e.RowIndex].Value

But this was always returning false even if the checkbox was checked. After scouring the web I found that you have to use

satelliteScenesDataGridView["inCartDataGridViewCheckBoxColumn", e.RowIndex].EditedFormattedValue

this returns the formatted value of the cell even if the value has not been commited.

Thursday, May 27, 2010

Generic GDI+ error when trying to save a bitmap

I was trying to read an image from a URL using webclient, but it kept erroring out with a GDI+ error when I was trying to save it. The image in question is Tiff, but the same code worked when it was JPG - strange!!

But anyway I was able to get around this issue after stumbling across the following MSDN article: http://support.microsoft.com/?id=814675

using (Stream ms = new MemoryStream(client.DownloadData("http://image.com")))
{
  Image img = Image.FromStream(ms);
  Bitmap bmp = img as Bitmap;
  Graphics g = Graphics.FromImage(bmp);
  Bitmap bmpNew = new Bitmap(bmp);
  g.DrawImage(bmpNew, new Point(0, 0));
  g.Dispose();
  bmp.Dispose();
  img.Dispose();

  bmpNew.Save("C:\\image.tiff");
}

Tuesday, May 25, 2010

Deleting an XML Node in C#

XML File



Code to remove an Order node
XmlDocument doc = new XmlDocument();
doc.Load(Properties.Settings.Default.OrderXMLFileLocation);
XmlNode node = doc.SelectSingleNode(string.Format("/CurrentOrders[Order='{0}']", orderNumber));
node.ParentNode.RemoveChild(node); 

Friday, May 21, 2010

Using XSD to generate classes

Its really nice to work with classes rather than parsing the XML structure using Xpath or the old fashioned XML reader. You can use XSD.exe to generate a c# class based on the XML document or XML schema and then use it to output an XML file.
1. Open VS command prompt.
2. CD to the location where your XSD file is.
3. Type in XSD -c -l:c# -n:[your namespace] [XSD filename]
This will generate a c# class. You can use that class along with the following code to output an XML file

XmlSerializer xmlSerializer = new XmlSerializer(typeof([Your Class]));
using (XmlWriter writer = XmlWriter.Create("[Path]"))
{
 xmlSerializer.Serialize(writer, [instance of your class]);
writer.Close(); }

Monday, May 17, 2010

Tortoise SVN Global Ignore Pattern for Visual Studio

I spend about 10 minutes everytime I install Tortoise SVN. So as a reminder to myself and anyone else who is interested - the global ignore pattersn for Tortoise:
*bin *obj RECYCLER Bin *.user *.suo *.dcu __history ModelSupport_* *.rsm thumbs.db