datagridview from xml

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Carew

    datagridview from xml

    Hi,

    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.

    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 column contains 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.

    Can anybody spot the bug in my code?

    TIA

    Regards
    Mark

    -------------- snip --------------------------
    private void btnIgnoreListCl ick(object sender, System.EventArg s 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.Su spendLayout();

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

    dataGridForm.Te xt = "Exclude List";

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

    dataGridForm.He lpButton = true;

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

    dataGridForm.Fo rmBorderStyle = FormBorderStyle .FixedDialog;

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

    dataGridForm.Ma ximizeBox = false;

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

    dataGridForm.Mi nimizeBox = false;

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

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

    dataGridForm.St artPosition = FormStartPositi on.CenterScreen ;

    /* parameterize the browse */

    dataGridForm.Si ze = 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.Co ntrols.Add(butt on1);

    /* Set the position of the button1 */

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

    /* Add button2 to the form. */

    dataGridForm.Co ntrols.Add(butt on2);

    /* Set the position of the button2 */

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

    /* set the buttons to particular events */

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

    dataGridForm.Ac ceptButton = button1;

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

    dataGridForm.Ca ncelButton = button2;

    /* create and position the datagridview */

    System.Windows. Forms.DataGridV iew ignoreDataGridV iew = new DataGridView();

    ignoreDataGridV iew.SuspendLayo ut();

    /* don't want row headers */

    ignoreDataGridV iew.RowHeadersV isible = false;

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

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

    ignoreDataGridV iew.TabIndex = 10;

    ignoreDataGridV iew.ColumnCount = 1;

    /* style */

    DataGridViewCel lStyle style = new DataGridViewCel lStyle();

    style.BackColor = Color.Bisque;

    style.Selection BackColor = Color.LightBlue ;

    style.ForeColor = Color.Navy;

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

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

    ignoreDataGridV iew.DefaultCell Style = style;

    /* data source connection */

    ignoreDataGridV iew.AutoGenerat eColumns = true;

    /* completely fill the parent controls' canvas */

    ignoreDataGridV iew.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.ReadXmlSchem a(@"c:\csharp\i gnoreList.xsd") ;

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

    ds.ReadXml(@"c: \csharp\ignoreL ist.xml",XmlRea dMode.ReadSchem a);

    /*

    // debugging

    foreach (DataTable table in ds.Tables)

    {

    MessageBox.Show (table.TableNam e.ToString());

    }

    */

    /* associate xml table with datgridview */

    /* either or */

    // ignoreDataGridV iew.DataSource = ds.Tables[0].DefaultView;

    ignoreDataGridV iew.DataSource = ds;

    ignoreDataGridV iew.DataMember = "ignoreDirector y";

    ignoreDataGridV iew.SelectionMo de = DataGridViewSel ectionMode.RowH eaderSelect;

    ignoreDataGridV iew.CellContent Click +=

    new DataGridViewCel lEventHandler(t his.ignoreDataG ridView_CellCon tentClick);

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

    DataGridViewCol umn column1 = ignoreDataGridV iew.Columns[0];

    column1.HeaderT ext = "Holy Cat";

    /* add the brows to the form */

    dataGridForm.Co ntrols.Add(igno reDataGridView) ;

    /* Display the browse in the form. */

    dataGridForm.Re sumeLayout();

    ignoreDataGridV iew.ResumeLayou t();

    dataGridForm.Sh owDialog();

    ignoreDataGridV iew.Show();

    dataGridForm.Di spose();

    }

    private void ignoreDataGridV iew_CellContent Click(object sender, DataGridViewCel lEventArgs e)

    {

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

    }

    --------------- end snip --------------

Working...