Re: I Think I Found The Problem
- From: "Dave Sexton" <dave@jwa[remove.this]online.com>
- Date: Wed, 22 Nov 2006 02:15:35 -0500
Hi Warren,
I think you might have a bug in your code somewhere since I can't reproduce
the behavior that you're observing. I've included the sample project that I
used to try the repro, after my signature.
--
Dave Sexton
{ TestForm.Designer.cs }
partial class TestForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed;
otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cbOpenJobs = new System.Windows.Forms.ComboBox();
this.lblUnsorted = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.lblSort = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.lblSelected = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label5 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// cbOpenJobs
//
this.cbOpenJobs.FormattingEnabled = true;
this.cbOpenJobs.Location = new System.Drawing.Point(133, 27);
this.cbOpenJobs.Name = "cbOpenJobs";
this.cbOpenJobs.Size = new System.Drawing.Size(85, 19);
this.cbOpenJobs.TabIndex = 0;
//
// lblUnsorted
//
this.lblUnsorted.AutoSize = true;
this.lblUnsorted.Location = new System.Drawing.Point(42, 170);
this.lblUnsorted.Name = "lblUnsorted";
this.lblUnsorted.Size = new System.Drawing.Size(82, 11);
this.lblUnsorted.TabIndex = 1;
this.lblUnsorted.Text = "lblUnsorted";
//
// label2
//
this.label2.AutoSize = true;
this.label2.DataBindings.Add(new System.Windows.Forms.Binding("Text",
this.cbOpenJobs, "SelectedValue", true));
this.label2.Location = new System.Drawing.Point(94, 25);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(47, 11);
this.label2.TabIndex = 2;
this.label2.Text = "label2";
//
// lblSort
//
this.lblSort.AutoSize = true;
this.lblSort.Location = new System.Drawing.Point(45, 265);
this.lblSort.Name = "lblSort";
this.lblSort.Size = new System.Drawing.Size(54, 11);
this.lblSort.TabIndex = 1;
this.lblSort.Text = "lblSort";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(43, 250);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(47, 11);
this.label1.TabIndex = 3;
this.label1.Text = "Sorted";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(42, 155);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(61, 11);
this.label3.TabIndex = 3;
this.label3.Text = "Unsorted";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(7, 25);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(89, 11);
this.label4.TabIndex = 4;
this.label4.Text = "Bound Guid: ";
//
// lblSelected
//
this.lblSelected.AutoSize = true;
this.lblSelected.Location = new System.Drawing.Point(8, 44);
this.lblSelected.Name = "lblSelected";
this.lblSelected.Size = new System.Drawing.Size(82, 11);
this.lblSelected.TabIndex = 4;
this.lblSelected.Text = "lblSelected";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.lblSelected);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Location = new System.Drawing.Point(45, 52);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(384, 92);
this.groupBox1.TabIndex = 5;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Selection";
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(45, 30);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(82, 11);
this.label5.TabIndex = 6;
this.label5.Text = "Job Number:";
//
// TestForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 11F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(485, 394);
this.Controls.Add(this.label5);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblSort);
this.Controls.Add(this.lblUnsorted);
this.Controls.Add(this.cbOpenJobs);
this.Font = new System.Drawing.Font("Lucida Console", 8.25F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)
(0)));
this.Name = "TestForm";
this.Text = "TestForm";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ComboBox cbOpenJobs;
private System.Windows.Forms.Label lblUnsorted;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label lblSort;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label lblSelected;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label5;
}
{ TestForm.cs }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
public partial class TestForm : Form
{
private DataTable table;
public TestForm()
{
InitializeComponent();
DataSet data = new DataSet();
table = data.Tables.Add();
table.Columns.Add("JobID", typeof(Guid));
table.Columns.Add("JobNumber", typeof(int));
table.Rows.Add(Guid.NewGuid(), 1);
table.Rows.Add(Guid.NewGuid(), 2);
table.Rows.Add(Guid.NewGuid(), 3);
table.Rows.Add(Guid.NewGuid(), 4);
table.Rows.Add(Guid.NewGuid(), 5);
table.DefaultView.Sort = "JobID";
StringBuilder tableInfo = new StringBuilder();
foreach (DataRow row in table.Rows)
{
tableInfo.AppendFormat("Job {0}; ID={1}{2}",
row["JobNumber"], row["JobID"], Environment.NewLine);
}
lblUnsorted.Text = tableInfo.ToString();
tableInfo = new StringBuilder();
int index = 0;
foreach (DataRowView rowView in table.DefaultView)
{
tableInfo.AppendFormat("Index: {1}; Job: {2}; ID={3}{0}",
Environment.NewLine, index++, rowView["JobNumber"], rowView["JobID"]);
}
lblSort.Text = tableInfo.ToString();
cbOpenJobs.SelectedIndexChanged += cbOpenJobs_SelectedIndexChanged;
cbOpenJobs.DisplayMember = "JobNumber";
cbOpenJobs.ValueMember = "JobID";
cbOpenJobs.DataSource = table.DefaultView;
}
private void cbOpenJobs_SelectedIndexChanged(object sender, EventArgs e)
{
Guid jobID = (Guid) cbOpenJobs.SelectedValue;
int foundRowIndex = table.DefaultView.Find(jobID);
int jobNumber = (int) table.DefaultView[foundRowIndex]["JobNumber"];
lblSelected.Text = string.Format(
"Found At Index: {1}{0}Job {2}{0}ID: {3}",
Environment.NewLine, foundRowIndex, jobNumber, jobID);
}
}
"Warren J. Hairston" <whairston@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:ezrwb8fDHHA.4312@xxxxxxxxxxxxxxxxxxxxxxx
Dave,
I think I found the problem - and it has nothing at all to do with setting
DataSource first (or last) or casting the Guid that is returned. My code
was returning the Guids properly, although I did implement the casting
technique that you suggested.
The problem is the line that reads:
DV.Sort = "JobID";
It appears that by sorting the DataView on a database Guid prior to
binding, the combo box rearranges the association between the values (the
JobID Guids) and the display members (the JobNumbers). I assume this is
because database Guids are sorted using only the last 6 bytes??? Or maybe
the DataView and the combo box are conflicting with each other by trying
to sort the data by different fields??? By looking at the debugger, it
appears that once bound to the combo box, the JobIDs are arranged in
"database" order but the JobNumbers (the DisplayMembers) are in
alphabetical order - unfortunately, this causes the wrong JobID to be
associated with each JobNumber.
Changing the line above to:
DV.Sort = "JobNumber";
resolved the problem.
When I have more time, I'll test the code with this line removed as well.
Now I'm just unsure whether this is a bug or by design. If the SQL query
that populates your DataView sorts the data on a different field than the
DisplayMember of the combo box it is later bound to, will the same problem
occur?
I'll research it more after I finish my project.
Thanks!
- Warren
.
- Follow-Ups:
- Re: I Think I Found The Problem
- From: Warren J. Hairston
- Re: I Think I Found The Problem
- References:
- DataView.Find not working with GUID
- From: Warren J. Hairston
- Re: DataView.Find not working with GUID
- From: Dave Sexton
- Re: DataView.Find not working with GUID
- From: Warren J. Hairston
- Re: DataView.Find not working with GUID
- From: Dave Sexton
- Re: DataView.Find not working with GUID
- From: Dave Sexton
- Re: DataView.Find not working with GUID
- From: Warren J. Hairston
- Re: DataView.Find not working with GUID
- From: Dave Sexton
- I Think I Found The Problem
- From: Warren J. Hairston
- DataView.Find not working with GUID
- Prev by Date: Re: DataView.Find not working with GUID
- Next by Date: Re: MS Access DAO -> ADO.NET Migration
- Previous by thread: I Think I Found The Problem
- Next by thread: Re: I Think I Found The Problem
- Index(es):
Relevant Pages
|