简体   繁体   中英

How to display a datatable when Yes button is clicked?

How to display a datatable when Yes button is clicked? In my code the table does not show up.

Here is my code

DialogResult result1 = MessageBox.Show("Would you like to make changes?",
                                       "Context", MessageBoxButtons.YesNo);
if (result1 == DialogResult.Yes)
{ 
    DataTable dt = new DataTable ();
    dt.Columns.Add("Temperature");
    dt.Columns.Add("Dimensions");
    dt.Columns.Add("Yield Strength");
    dt.Columns.Add("Weight");
    dt.Columns.Add("Material");
    DataRow _salta = dt.NewRow ();             
}
if (result1 == DialogResult.No)
{
    this.Close();
}

To get a really quick display of the data in your DataTable you can create a form with a DataGridView .

Here is an example, which assumes that you have a filled DataTable and want to show all its data, ie all its columns and rows:

Form form = new Form();                // a blank form
DataGridView DGV = new DataGridView(); // a blank DataGridView
DGV.Parent = form;                     // we add the DGV to the from
DGV.AutoGenerateColumns = true;        // to copy all columns from the DataSource
DGV.DataSource = dt;                   // set the datasource to the table
form.Width = 500;                      // some size, change to your needs!
DGV.Dock = DockStyle.Fill;             // the DGV fills the form
        form.ShowDialog();             // we show it as a dialog

You should improve on this by at the very least setting the column widths to suitable values:

DGV.Columns[0].Width = 40;
DGV.Columns[1].Width = 50;
DGV.Columns[2].Width = 75;
// ..

Instead of setting the width indiviually you can also use one of the many automatic modes:

DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader;

Do test them all to see if any one of them works well for you!! (Hint: Sometimes it helps to play a little with the column headers)

In the same way you may want to set some formatting for some columns:

DGV.Columns[3].DefaultCellStyle.BackColor = Color.LightSalmon;
DGV.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
DGV.Columns[0].DefaultCellStyle.Format = "#00.00°";

Note that the numeric format of the last line can only work if the Column.DataType in your DataTable acutally is a numeric type! To enforce it you can use a line like this:

 yourDataTable.Columns[0].DataType = typeof(float);

And if it is only for display we should make it read-only:

DGV.ReadOnly = true;

If instead you want to display it as a means to edit the data you will probably need a lot more code; in this case I suggest to create a dedicated form and code all the necessary things like validation code, look-ups, proper error messages etc.. Also a Cancel button, undo and what not.. This is way beyod the scope of the question, though!

For the simplest editing demands the solution as shown will work, though!


Also note that the question as written creates an empty DataTable with some columns only. Do move the table to class level instead, so you can hold and maybe edit the data!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM