I have a DataTable dt1 that contains this columns : PRODUCT_ID,MIN_VALUE,MAX_VALUE,AMOUNT
and another DataTable dt2 that contains this columns : ID,MIN,MAX,POINT_TO_ADD
dt1 contains multiple rows that I want to copy them to dt2 how can I do that ?
try this
foreach (DataRow sourcerow in dt1.Rows)
{
DataRow destRow = dt2.NewRow();
destRow["ID"] = sourcerow["PRODUCT_ID"];
destRow["MIN"] = sourcerow["MIN_VALUE"];
destRow["MAX"] = sourcerow["MAX_VALUE"];
destRow["POINT_TO_ADD"] = sourcerow["AMOUNT"];
dt2.Rows.Add(destRow);
}
Try this:
for(int i=0;i<dt1.Rows.Count;i++){
DataRow dr = dt2.NewRow();
dr["ID"] = dt1.Rows[i]["PRODUCT_ID"];
dr["MIN"] = dt1.Rows[i]["MIN_VALUE"];
dr["MAX"] = dt1.Rows[i]["MAX_VALUE"];
dr["POINT_TO_ADD"] = dt1.Rows[i]["AMOUNT"];
dt2.Rows.Add(dr);
}
Related
I need to store datagridview selected rows into datatable in single columns like below
DataGridView
user1 user2 user3
ram sam ravi
DataTable
values
ram
sam
ravi
I tried below code
DataTable dt = new DataTable();
dt.Columns.Add("values",typeof(string));
foreach (DataGridViewRow gridRow in dataGridView_settings.Rows)
{
DataRow dtRow = dt.NewRow();
for (int i = 0; i < dataGridView_settings.Columns.Count; i++)
{
dtRow[0] = gridRow.Cells[i].Value;
dt.Rows.Add(dtRow);
}
}
Problem is its adding only one row in the datatable then showing error "This row already belongs to this table."
You should move the DataRow creation to the inner for
DataTable dt = new DataTable();
dt.Columns.Add("values",typeof(string));
foreach (DataGridViewRow gridRow in dataGridView_settings.Rows)
{
for (int i = 0; i < dataGridView_settings.Columns.Count; i++)
{
DataRow dtRow = dt.NewRow();
dtRow[0] = gridRow.Cells[i].Value;
dt.Rows.Add(dtRow);
}
}
I want to Delete the Multiple records from the DataTable
For example :
in my case PaperId is Repeating several Times.I want to Delete it all Duplicate records.
i have written code but loop is giving error
DataSet ds = new DataSet();
sqlDad.Fill(ds);
DataTable dt1 = new DataTable();
ds.Tables.Add(dt1);
dt1 = ds.Tables[0];
DataTable dt2 = new DataTable();
dt2 = dt1;
List<DataRow> rowsToDelete = new List<DataRow>();
foreach(DataRow dr in ds.Tables[0].Rows)
{
int r = ds.Tables[0].Columns.Count;
string x = dr.ItemArray[0].ToString();
int counter = 0;
foreach (DataRow dr1 in ds.Tables[0].Rows)
{
if (x == dr1.ItemArray[0].ToString())
{
counter++;
}
if (counter > 1)
{
rowsToDelete.Add(dr1);
foreach (DataRow row in rowsToDelete)
{
dt2.Rows.Remove(row);
}
dt2.AcceptChanges();
rowsToDelete.Clear();
}
}
Using the DefaultView of the DataTable and setting the sort order on the column that you don't want repeats to appear. You could loop over the rows and delete all the rows after the first one
// Work on the first table of the DataSet
DataTable dt1 = ds.Tables[0];
// No need to work if we have only 0 or 1 rows
if(dt1.Rows.Count <= 1)
return;
// Setting the sort order on the desidered column
dt1.DefaultView.Sort = dt1.Columns[0].ColumnName;
// Set an initial value ( I choose an empty string but you could set to something not possible here
string x = string.Empty;
// Loop over the row in sorted order
foreach(DataRowView dr in dt1.DefaultView)
{
// If we have a new value, keep it else delete the row
if(x != dr[0].ToString())
x = dr[0].ToString();
else
dr.Row.Delete();
}
// Finale step, remove the deleted rows
dt1.AcceptChanges();
Try This
DataRow[] rows;
rows=dataTable.Select("UserName = 'ABC'"); // UserName is Column Name
foreach(DataRow r in rows)
r.Delete();
If you want to remove the entire row from DataTable ,
try this
DataTable dt = new DataTable(); //User DataTable
DataRow[] rows;
rows = dt.Select("UserName = 'KarthiK'");
foreach (DataRow row in rows)
dt.Rows.Remove(row);
I have multiple datatable. I want to show all the datatable rows into a single gridview.
How can I do that?
DataTable dtbag101 = (DataTable)Session["bag101"];
DataTable dtwallet111 = (DataTable)Session["wallet111"];
DataSet ds= new DataSet();
ds.Tables.Add(dtbag101);
ds.Tables.Add(dtwallet111);
GridView1.DataSource= ds;
GridView1.DataBind();
The column names for both datatable are the same.
Here I was trying to use dataset but only first DataTable i.e. datbag101 was showing in the gridview.
How can I show all the values in one gridview?
Provided that your two data tables have the same columns, you can UNION them with some handy LINQ.
DataTable dtbag101 = (DataTable)Session["bag101"];
DataTable dtwallet111 = (DataTable)Session["wallet111"];
var result = dtbag101.AsEnumerable().Union(dtwallet111.AsEnumerable());
GridView1.DataSource = result;
GridView1.DataBind();
Otherwise try use DataTable.Merge:
DataTable dtbag101 = (DataTable)Session["bag101"];
DataTable dtwallet111 = (DataTable)Session["wallet111"];
dtbag101.Merge(dtwallet111, true);
GridView1.DataSource = dtbag101;
GridView1.DataBind();
I'm not sure why this isn't working for you. Try this method (grabbed from here):
public static DataTable Union(DataTable First, DataTable Second)
{
//Result table
DataTable table = new DataTable("Union");
//Build new columns
DataColumn[] newcolumns = new DataColumn[First.Columns.Count];
for(int i=0; i < First.Columns.Count; i++)
{
newcolumns[i] = new DataColumn(
First.Columns[i].ColumnName, First.Columns[i].DataType);
}
table.Columns.AddRange(newcolumns);
table.BeginLoadData();
foreach(DataRow row in First.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
foreach(DataRow row in Second.Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
table.EndLoadData();
return table;
}
Call the method with your two datatables:
GridView1.DataSource = Union(dtbag101, dtwallet111);
You can simply use DataTable.Merge method
DataTable dtbag101 = (DataTable)Session["bag101"];
DataTable dtwallet111 = (DataTable)Session["wallet111"];
dtbag101.Merge(dtwallet111); //Merge action
GridView1.DataSource= dtbag101;
GridView1.DataBind();
I dont know much about this. Just referred it now.
or else
try for loop
DataSet ds = new DataSet();
addTables(dtbag101);
addTables(dtwallet111); //ds will be merge of both tables here
private void addTables(DataTable dt)
{
for(int intCount = ds.Tables[0].Rows.Count; intCount < dt.Rows.Count;intCount++)
{
for(int intSubCount = 0;intSubCount < dt.Columns.Count; intSubCount++)
{
ds.Tables[0].Rows[intCount][intSubCount] = dt.Rows[intCount][intSubCount];
}
}
}
You may need to use DataTable.Merge
DataTable dtAll = new DataTable();
dtAll = dtbag101 .Copy();
dtAll.Merge(dtwallet111, true);
GridView1.DataSource= dtAll;
GridView1.DataBind();
Edit to show how it should work
private void BindGridWithMergeTables()
{
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Columns.Add("ID");
dt2.Columns.Add("ID");
DataRow dr1 = dt1.NewRow();
DataRow dr2 = dt2.NewRow();
dr1["ID"] = "1";
dr2["ID"] = "2";
dt1.Rows.Add(dr1);
dt2.Rows.Add(dr2);
DataTable dtAll = new DataTable();
dtAll = dt1.Copy();
dtAll.Merge(dt2, true);
dataGridView1.DataSource = dtAll;
dataGridView1.DataBind();
}
I have one datatable which has four or five columns. I dont know exactly the columns name and its count. But I want to bind the first row of the datatable into the GridView. How to do this? I need all your suggestions please.
Linq should be helpful here to get first item.
var Temp = dt.AsEnumerable().Take(1).CopyToDataTable();
use the filter in the datatable :
dt.Select("ID = 1");
you can try like this..
dt = new DataTable();
dt_Property.Columns.Add("Field1");
int i = 0;
DataRow row = null;
foreach (DataRow r in ds.Tables[0].Rows)
{
row = dt.NewRow();
row["Field1"] = ds.Tables[0].Rows[i][1];
dt_Property.Rows.Add(row);
i = i + 1;
}
dataGridView1.DataSource = dt;
Im new in asp.net. I want to know how to add a row in a gridview programatically. I was able to do it but it just displays the latest addition.
Here is my code:
DataTable dt = new DataTable();
dt.Columns.Add("Question");
dt.Columns.Add("Answer");
DataRow dr = dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();
gvQnA.DataSource = dt;
gvQnA.DataBind();
Its because you are creating new table each time and binding it with the grid
Do code as below may resolve your issue ...
here i am taking existing datasource and binding it again by adding two more row...
DataTable dt = gridView.DataSource as DataTable;
if (dt != null)
{
DataRow dr = dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
dt.Rows.Add(dr);
dt.AcceptChanges();
gvQnA.DataSource = dt;
gvQnA.DataBind();
}
#Pranay is correct.In addition You can also achive that by using DataTable as property.
private DataTable Dt
{
set { ViewState.Add("Dt", value); }
get { return (DataTable)ViewState["Dt"]; }
}
...
DataRow dr = Dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
Dt.Rows.Add(dr);
Dt.AcceptChanges();
gvQnA.DataSource = Dt;
gvQnA.DataBind();
You have added one row in your code thats why it is showing one row.
If you have added multiple rows it would have shown proper result
DataTable dt = new DataTable();
dt.Columns.Add("Question");
dt.Columns.Add("Answer");
DataRow dr = dt.NewRow();
dr["Question"] = txtQuestion.Text;
dr["Answer"] = txtAnswer.Text;
dt.Rows.Add(dr);
**DataRow dr = dt.NewRow();
dr["Question"] = "2nd row";
dr["Answer"] = "2nd row";
dt.Rows.Add(dr);**
dt.AcceptChanges();
gvQnA.DataSource = dt;
gvQnA.DataBind();
May be #Pranay is also right
Hey Just check this. This might help u
DataTable dataTable = new DataTable();
int columnsCount = // Set the number of the table's columns here.
for (int columnIndex = 0; columnIndex < columnsCount; columnIndex++)
{
DataColumn dataColumn = new DataColumn();
// Assign dataColumn properties' values here ..
dataTable.Columns.Add(dataColumn);
}
int rowsCount = // Set the number of the table's rows here.
for (int columnIndex = 0; columnIndex < columnsCount; columnIndex++)
{
DataRow dataRow = new DataRow();
dataRow["ColumnName"] = // Set the value here ..
dataTable.Rows.Add(dataRow);
}