468,278 Members | 1,562 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,278 developers. It's quick & easy.

datagridview from xml

Hi,

Problem - An extra column to the left (even with row headers disabled)

----------------------------------------------------------
Apologies if this posting is already in the newsgroup; but have changed to
40tude and can't see it in the newly dowloaded messages. It was originally
html (tut tut) so maybe it didn't register. I have tried a new xml file and
generated a new xsd file from it using xsd.exe and now the xml file
produces three columns. The first contains no data and the other two
contain the data in the xml file. So I am now getting 3 columns for 2 data
elements. Does the etiquette of this news group allow attachment of the xml
and xsd files?
----------------------------------------------------------

Original posting

I'm writing a winform app (as practice) that has as one of its features a
button that reads an xml file to a datagridview.

No matter what I do I get two columns instead of one.
(Now three instaed of two)

The "first column", I retrieve and place the text "Holy Cat" in its header
text. This column contains no data from the xml file.

The second (and now third) columns contain the correct data from the xml
file.

When clicked the "first column" is not shown as a column by the event
delegate; but the "second column" reports that it is column 1. and the
third column is reported as column 3.

Can anybody spot the bug in my code?

TIA

Regards
Mark

-------------- snip --------------------------
private void btnIgnoreListClick(object sender, System.EventArgs e)

{

int formDown = 300;

int formAcross = 400;

/* create a new form to hold a datagridview */

System.Windows.Forms.Form dataGridForm = new Form();

/* suspend display till later */

dataGridForm.SuspendLayout();

/* Set the caption text of the form. */

dataGridForm.Text = "Exclude List";

/* Display a help button on the form. */

dataGridForm.HelpButton = true;

/* Define the border style of the form to a dialog box. */

dataGridForm.FormBorderStyle = FormBorderStyle.FixedDialog;

/* Set the MaximizeBox to false to remove the maximize box. */

dataGridForm.MaximizeBox = false;

/* Set the MinimizeBox to false to remove the minimize box. */

dataGridForm.MinimizeBox = false;

/* Set the accept button of the form to button1. */

/* Set the start position of the form to the center of the screen. */

dataGridForm.StartPosition = FormStartPosition.CenterScreen;

/* parameterize the browse */

dataGridForm.Size = new System.Drawing.Size(formAcross,formDown);

/* Create two buttons to use as the accept and cancel buttons. */

System.Windows.Forms.Button button1 = new Button ();

System.Windows.Forms.Button button2 = new Button ();

/* Set the text of button1 to "OK" and "Cancel" */

button1.Text = "OK";

button2.Text = "Cancel";

/* Add button1 to the form. */

dataGridForm.Controls.Add(button1);

/* Set the position of the button1 */

button1.Location = new Point (formAcross-200,formDown-60);

/* Add button2 to the form. */

dataGridForm.Controls.Add(button2);

/* Set the position of the button2 */

button2.Location = new Point (formAcross-300,formDown-60);

/* set the buttons to particular events */

/* Set the accept button of the form to button1. */

dataGridForm.AcceptButton = button1;

/* Set the cancel button of the form to button2. */

dataGridForm.CancelButton = button2;

/* create and position the datagridview */

System.Windows.Forms.DataGridView ignoreDataGridView = new DataGridView();

ignoreDataGridView.SuspendLayout();

/* don't want row headers */

ignoreDataGridView.RowHeadersVisible = false;

ignoreDataGridView.Location = new System.Drawing.Point(10,10);

ignoreDataGridView.Size = new System.Drawing.Size(formAcross-20,
formDown-100);

ignoreDataGridView.TabIndex = 10;

ignoreDataGridView.ColumnCount = 1;

/* style */

DataGridViewCellStyle style = new DataGridViewCellStyle();

style.BackColor = Color.Bisque;

style.SelectionBackColor = Color.LightBlue;

style.ForeColor = Color.Navy;

style.Font = new Font("Arial",8,FontStyle.Bold);

style.Padding = new Padding(5,2,5,5);

ignoreDataGridView.DefaultCellStyle = style;

/* data source connection */

ignoreDataGridView.AutoGenerateColumns = true;

/* completely fill the parent controls' canvas */

ignoreDataGridView.Dock = DockStyle.Top;

/* Create a data set to hold IgnoreList */

DataSet ds = new DataSet();

/* Read in the schema that describes the ignoreList.xml file */

ds.ReadXmlSchema(@"c:\csharp\ignoreList.xsd");

/* Read in the data from the xml file */

ds.ReadXml(@"c:\csharp\ignoreList.xml",XmlReadMode .ReadSchema);

/*

// debugging

foreach (DataTable table in ds.Tables)

{

MessageBox.Show(table.TableName.ToString());

}

*/

/* associate xml table with datgridview */

/* either or */

// ignoreDataGridView.DataSource = ds.Tables[0].DefaultView;

ignoreDataGridView.DataSource = ds;

ignoreDataGridView.DataMember = "ignoreDirectory";

ignoreDataGridView.SelectionMode =
DataGridViewSelectionMode.RowHeaderSelect;

ignoreDataGridView.CellContentClick +=

new DataGridViewCellEventHandler(this.ignoreDataGridVi ew_CellContentClick);

/* set the header text of the first column */

DataGridViewColumn column1 = ignoreDataGridView.Columns[0];

column1.HeaderText = "Holy Cat";

/* add the brows to the form */

dataGridForm.Controls.Add(ignoreDataGridView);

/* Display the browse in the form. */

dataGridForm.ResumeLayout();

ignoreDataGridView.ResumeLayout();

dataGridForm.ShowDialog();

ignoreDataGridView.Show();

dataGridForm.Dispose();

}

private void ignoreDataGridView_CellContentClick(object sender,
DataGridViewCellEventArgs e)

{

MessageBox.Show("Number of Column"+ e.ColumnIndex.ToString());

}

--------------- end snip --------------
Feb 2 '06 #1
1 4327

The (temporary) workaround is to set the extra column to Visible = false;

DataGridViewColumn column1 = ignoreDataGridView.Columns[0];

column1.Visible = false;

Feb 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by DraguVaso | last post: by
10 posts views Thread by Henok Girma | last post: by
7 posts views Thread by Mitchell S. Honnert | last post: by
7 posts views Thread by =?Utf-8?B?TG9zdEluTUQ=?= | last post: by
reply views Thread by jeastman - Hotmail | last post: by
3 posts views Thread by Andrus | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.