Merged with Add new Row to DataTable.
I am trying to add two new Rows to a DataTable. My DataSource consist of a Database with one Table (FooTable) and one column (FooName).
See following code. Only the first Row "Mary" is added and displayed on my DataGrid, but not the second row "Jesus". No exception occurs. Why doesn't it work and what is the correct way to add multiple rows dynamically to DataTable?
/* dg1 = DataGrid; ds = DataSet */
dg1.ItemsSource = ds.Tables;
/* dt = DataTable */
DataRow newRow = dt.NewRow();
newRow["FooName"] = "Mary";
dt.Rows.Add(newRow);
/* add another row to data table */
DataRow newRow2 = dt.NewRow();
newRow2["FooName"] = "Jesus";
dt.Rows.Add(newRow2);
Related
I have a gridcontrol which needs to be populated with some data.
I have created a datatable and populated data in datarow and added to the table. Data is present in table on analysis. Datatable is then set as datasource for grid.
Code is below.
DataTable table = new DataTable();
table.Columns.Add("Tran Date");
table.Columns.Add("Due Date");
table.Columns.Add("Voucher");
table.Columns.Add("Vr No");
table.Columns.Add("Bill Amount");
table.Columns.Add("OutStanding");
table.Columns.Add("Remittance");
table.Columns.Add("Balance");
for (intcount=0;intcount<=BackupFunctions.mdtAgeingTemp.Rows.Count - 1;intcount++)
{
DataRow row = table.NewRow();
row["Tran Date"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][1];
row["Due Date"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][2];
row["Voucher"]= BackupFunctions.mdtAgeingTemp.Rows[intcount][3];
row["Vr No"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][4];
row["Bill Amount"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][10];
row["OutStanding"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][5];
row["Remittance"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][6];
row["Balance"] = BackupFunctions.mdtAgeingTemp.Rows[intcount][7];
table.Rows.Add(row);
}
grdAgeing.DataSource = table;
Its found that 5 rows which is the result is displaying but they are empty. Could you figure out the issue in my code.
This is the code to bind the data to the datatable
dtPromotion = db.GetDatastrong text(drcheck["vb"].ToString());
DataRow reference
//create the new row
DataRow newRow = dtPromotion.NewRow();
//Set the columns
newRow["Col1"] = "data";
newRow["Col2"] = "data";
//add the row to the data table
dtPromotion.Rows.Add(newRow);
My problem is to transfer selected rows from data grid view to another data grid view. I don't know what to use if I use the primary key and get the values from database to the another database or send the selected row's data from datagridview1 to datagridview2
You can create a new DataTable and insert all the selected rows to that DataTable
DataTable GetSelectedRows(DataGridView dgv)
{
var dt = new DataTable();
foreach (DataGridViewColumn column in dgv.Columns)
{
if (column.Visible)
{
// You could potentially name the column based on the DGV column name (beware of dupes)
// or assign a type based on the data type of the data bound to this DGV column.
dt.Columns.Add();
}
}
object[] cellValues = new object[dgv.Columns.Count];
foreach (DataGridViewRow row in dgv.Rows)
{
if (!row.Selected) continue; // Add only Selected Rows
for (int i = 0; i < row.Cells.Count; i++)
cellValues[i] = row.Cells[i].Value;
dt.Rows.Add(cellValues);
}
return dt;
}
You can pass all SelectedRows to new DataGridView using this
dataGridView2.DataSource = GetSelectedRows(dataGridView1)
I want to insert data row filter value to datatable but it returns the following error "This row already belongs to another table." Please help me to fix this error..
This is my partial code :
for (int j = 0; j < ListBox1.Items.Count; j++)
{
DataView DV = new DataView();
DV = DS1.DefaultView;
DV.RowFilter = "fldemployee='" + ListBox1.Items[j].Text + "' and fldassigndate = '04-07-2012'";
if (DV.Count > 0)
{
DataTable table = DV.ToTable("sp_getallattendancesetup");
DataRow row = table.Rows[0];
DT.Rows.Add(row);
}
}
GridView1.DataSource = DT;
GridView1.DataBind();
You can use ImportRow
DT.Rows.ImportRow(row);
Information from msdn: Calling NewRow adds a row to the table using
the existing table schema, but with default values for the row, and
sets the DataRowState to Added. Calling ImportRow preserves the
existing DataRowState along with other values in the row. If the
DataRow that is passed as a parameter is in a detached state, it is
ignored, and no exception is thrown.
The new row will be added to the end of the data table.
If the new row violates a Constraint it won’t be added to the data
table.
You can get the index of the new row with as DataTable.Rows.Find and
DataTable.Rows.IndexOf. See DataRowCollection and Rows for more
information.
Reference here
How can I add a new row in a DataGridView when using relations?
When the DataGridView is bound to a DataTable, I can add a row, but when Im using relations, nothing happens, the row won't add to theDataGridView`.
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("UserName",typeof(string));
ds.Tables.Add(dt);
dataGridView1.DataSource = dt;
DataRow dr = dt.NewRow();
dr["ID"] = 1;
dr["UserName"] = "maxWhite";
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
before this i use relations, ds.Relations.Add(new DataRelation ..........
and after this i can`t add row
dataGridView3.DataSource = ds.Tables[0];
dataGridView3.DataMember = "relation_name";
because i must use data binding to filter out my result, eg. i click on parent datagrid 1 and in datagrid 2 i see child rows
e.g. i click in parent grid Ford in child grid shows all Ford models, then on Mazda and so on, and i can`t add row to child grid, because i have relation, when i delete relation all i fine.