datagridview from xml

M

mark carew

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.ignoreDataGridView_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 --------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top