I previously used datagridview but now changed it to use a datatable
`
private void table()
{
//create a data set
DataSet ds = new DataSet();
//create a data table for the data set
DataTable dt = new DataTable();
//create some columns for the datatable
DataColumn dc = new DataColumn("Name");
DataColumn dc2 = new DataColumn("Entry");
DataColumn dc3 = new DataColumn("SL");
DataColumn dc4 = new DataColumn("SL%");
DataColumn dc5 = new DataColumn("TP");
DataColumn dc6 = new DataColumn("TP%");
DataColumn dc7 = new DataColumn("Position");
DataColumn dc8 = new DataColumn("Day");
DataColumn dc9 = new DataColumn("Notes");
//add the columns to the datatable
dt.Columns.Add(dc);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Columns.Add(dc7);
dt.Columns.Add(dc8);
dt.Columns.Add(dc9);
//add the datatable to the datasource
ds.Tables.Add(dt);
//make this data the datasource of our gridview
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.AutoSize = true;
}
`
When using datagridview i used this code on a button click event to add a row
dataGridView1.Rows.Add(name, EntryPrice.Text, StopPrice.Text, slper, ProfitPrice.Text, tpper, pos, day, NotesTB.Text);
How do I add a row to the datatable with the same values using a button click event?
Using
dt.Rows.Add
isnt recognising the dt within my button click
Found an answer from another user
var devents = (DataTable)dataGridView3.DataSource;
DataRow newRow = devents.NewRow();
newRow["start_time"] = new_starttime;
newRow["stop_time"] = new_stoptime;
newRow["cycle_time"] = new_cycletime;
devents.Rows.Add(newRow);
Credit to #kekusemau
Hope this link helps you:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim table As DataTable = EEDataDataSet.Tables("Users")
' Use the NewRow method to create a DataRow
'with the table's schema.
Dim newuserrow As DataRow = table.NewRow()
' Set values in the columns:
newuserrow("Username") = Me.UsernameTextBox.Text
newuserrow("Password") = Me.PasswordTextBox.Text
' Add the row to the rows collection.
table.Rows.Add(newuserrow)
yourTableAdapter.updateAll(EEDataDataSet)
MsgBox("New User addition successful.")
Me.Close()
End Sub
https://social.msdn.microsoft.com/Forums/vstudio/en-US/eddc0789-67f8-46ff-a35a-01d67a5e4944/add-a-row-to-a-datatable-from-a-form-button-click?forum=vbgeneral
Related
When I try to add new column and use SetOrdinal(2)
The newly added column was positioned in the last column
conn = new MySqlConnection(connectString);
conn.Open();
fireAdapter = new MySqlDataAdapter(query, conn);
fireBuilder = new MySqlCommandBuilder(fireAdapter);
fireDataTable = new DataTable();
fireAdapter.Fill(fireDataTable);
fireSource = new BindingSource();
fireSource.DataSource = fireDataTable;
grid.DataSource = fireSource;
conn.Close();
DataColumn newcol = new DataColumn("Blah", typeof(string));
fireDataTable.Columns.Add(newcol);
newcol.SetOrdinal(2);
I'm pretty sure that your code does what you expect, it changes the position of the new column in the DataTable. But it is still shown as last column in the grid. So why you don't initialize the BindingSource after you have fully initialized the DataTable?
DataColumn newcol = new DataColumn("Blah", typeof(string));
fireDataTable.Columns.Add(newcol);
newcol.SetOrdinal(2);
// and now start assign it to the BindingSource
I have created a datatable then manually added two new columns in position 0 and 1, and given them default values. When I loop through the datatable it prints all the values out correctly and everything seems to be there. but when I pass the datatable to a DataGridView via a binding source it doesn't show the two new columns in the datagridview. Any idea as to what im doing wrong?
Regards
Amarino
DataTable lDT2 = Conn.ExecuteStoredProcedureValidation(lDT);
DataColumn newColumn1 = new DataColumn("TestName", typeof(string));
DataColumn newColumn2 = new DataColumn("SheetName", typeof(string));
newColumn1.DefaultValue = "test";
newColumn2.DefaultValue = "test2";
lDT2.Columns.Add(newColumn2);
lDT2.Columns.Add(newColumn1);
lDT2.Columns["TestName"].SetOrdinal(0);
lDT2.Columns["SheetName"].SetOrdinal(1);
DataGridView lDGV = new DataGridView();
lDGV.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing;
lDGV.RowHeadersVisible = false;
BindingSource BindingSource1 = new BindingSource(); //create new data binding source
BindingSource1.DataSource = lDT2; //SetData source change to LDT2
lDGV.DataSource = BindingSource1;
lDGV.RowHeadersVisible = true;
lDGV.Tag = page.Controls[0].Tag;
lDGV.AccessibleName = page.Controls[0].AccessibleName;
lFormV.tabControl_Val.TabPages[page.Name].Show();
lFormV.tabControl_Val.TabPages[page.Name].Controls.Add(lDGV);
I am trying to add new records to a data table. The data records will display in a data grid view and write to an XML file. Every time I click the buttonCreate a record is created but it overwrites the existing entry instead of adding a new record. How do I add new row to data table without overwriting the existing record?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCreate_Click(object sender, EventArgs e)
{
//create the DataTable
DataTable dt = new DataTable("Contact");
//dt.TableName = "Contact";
//create columns for the DataTable
DataColumn dc1 = new DataColumn("Id");
dc1.DataType = System.Type.GetType("System.Int32");
dc1.AutoIncrement = true;
dc1.AutoIncrementSeed = 1;
dc1.AutoIncrementStep = 1;
DataColumn dc2 = new DataColumn("Name");
DataColumn dc3 = new DataColumn("Age");
DataColumn dc4 = new DataColumn("Gender");
//add columns to the DataTable
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
DataRow row;
row = dt.NewRow();
row["Name"] = textBoxName.Text;
row["Age"] = textBoxAge.Text;
row["Gender"] = textBoxGender.Text;
dt.Rows.Add(row);
//insert data in the row
//dt.Rows.Add(null, textBoxName.Text, textBoxAge.Text, textBoxGender.Text);
dataGridView1.DataSource = dt;
//create DataSet
DataSet ds = new DataSet();
ds.DataSetName = "AddressBook";
ds.Tables.Add(dt);
ds.WriteXml("Contacts.xml");
}
}
Also, should I be creating the datatable in the form event?
Your problem is that you are recreating the datatable every time the button is clicked. You should declare the datatable as a property of the Form1 class, create the columns in the Form1() method (this is called by the .net framework when the form is created (i.e when you start your application)), and then create your new row on the button click, add it to the datatable, and reset the gridview to point at the datatable to refresh it.
//create the DataTable
DataTable dt = new DataTable("Contact");
DataSet ds;
public Form1()
{
InitializeComponent();
//create columns for the DataTable
DataColumn dc1 = new DataColumn("Id");
dc1.DataType = System.Type.GetType("System.Int32");
dc1.AutoIncrement = true;
dc1.AutoIncrementSeed = 1;
dc1.AutoIncrementStep = 1;
//add columns to the DataTable
dt.Columns.Add(dc1);
dt.Columns.Add(new DataColumn("Name"));
dt.Columns.Add(new DataColumn("Age"));
dt.Columns.Add(new DataColumn("Gender"));
//create DataSet
DataSet ds = new DataSet();
ds.DataSetName = "AddressBook";
ds.Tables.Add(dt);
}
private void buttonCreate_Click(object sender, EventArgs e)
{
DataRow row = dt.NewRow();
row["Name"] = textBoxName.Text;
row["Age"] = textBoxAge.Text;
row["Gender"] = textBoxGender.Text;
dt.Rows.Add(row);
dataGridView1.DataSource = dt;
ds.WriteXml("Contacts.xml");
}
The record keeps getting overwritten by the next record because of this line of code:
ds.WriteXml("Contacts.xml");
You need to append to the XML file, not override it. See this stackoverflow post.
If the application is quick and dirty then it's fine to do whatever you like inside the button handler. However if it is a real world application then you really need to have some sort of application architecture in place.
The following method is used by me to send all rows from a data grid view in a form(datagridview1 of form1) to another data grid view of another form(datagridview1 of form2) when a button is clicked.
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
DataTable dt1 = new DataTable();
f2.dataGridView1.DataSource = dt1;
foreach (DataGridView row in dataGridView1.Rows)
{
int n = f2.dataGridView1.Rows.Add();
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
f2.dataGridView1.Rows[n].Cells[col.Index].Value = dataGridView1.Rows[row.Index].Cells[col.Index].Value.ToString();
}
}
}
But no data is sent to datagridview1 of form2! how can i correct this?
Depending on the circumstances, I'd work with a data source, i.e. this way
VB.NET
Dim dt as New DataTable
dt = ds.Tables(0)
Me.DataGridView1.datasource = dt
Form2.DataGridView2.datasource = dt
C#
DataTable dt = new DataTable();
dt = ds.Tables(0);
this.DataGridView1.datasource = dt;
Form2.DataGridView2.datasource = dt;
If you wanted to modify one of them independently, you need a second datatatable:
Dim dt2 as New DataTable
dt2 = dt.Copy() ' copies datatable structure AND the data
Form2.DataGridView2.datasource = dt
I first want to add a database column in DataColumn..and then i have to add datacoloumn in datatable.. please guide me;
SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from exam_cyctstmarkdet",con);
DataColumn c = new DataColumn();
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
Is the bit you're missing
DataTable DT = new DataTable;
d2.Fill(DT);
DT.Columns.Add(c);
???
If you wish to create a DataTable, and add your own columns to it, you can try
DataTable dt = new DataTable("MyTable");
DataColumn col = dt.Columns.Add("parta", typeof(int));
You only have to keep a reference to the column if you plan to use it somewhere, else you can try
DataTable dt = new DataTable("MyTable");
dt.Columns.Add("parta", typeof(int));
I would suggest that you use the SqlDataAdapter to populate a DataSet, which will contain a DataTable. I assume you want to add the new column to this DataTable.
Here's how to do that:
SqlDataAdapter d2 = new SqlDataAdapter("select MARK_PARTA from exam_cyctstmarkdet",con);
DataSet dst = new DataSet();
d2.Fill(dst); // now you have a populated DataSet containing a DataTable
DataTable dt = dst.Tables[0]; // I created this variable for clarity; you don't really need it
DataColumn c = new DataColumn();
c.ColumnName = "parta";
c.DataType = System.Type.GetType("System.Int32");
dt.Add(c);
After you add the Column to the DataTable, the Column will be empty.
You can populate it in code.