datarow filter value to datatable - c#

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

Related

How to rows dynamically in datagrid C# wpf?

I want to set number of rows of datagrid equal to a number given by user in a textbox.
means if i enter 6 in the textbox, it should add 6 rows in the datagrid.
thats what i want. I may be wrong.
But Null exception is being thrown.
How to fix my issue?
Here is the code:
DataTable dt;
DataRow dr;
int rownumber=0;
dt = new DataTable("emp2");
DataColumn dc3 = new DataColumn("Wi", typeof(double));
DataColumn dc4 = new DataColumn("Hi", typeof(double));
rownumber = Int32.Parse(txtBexNumber.Text);
dr[rownumber] = dt.NewRow();
dt.Rows.Add(dr);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
datagrid1.ItemsSource = dt.DefaultView;
You're creating a DataTable, then trying to immediately access a user-specified row in the table even though no rows have been added yet.
Use a loop to add rows first
for(int i = 0; i < rowNumber; i++)
{
dr = dt.NewRow();
dt.Rows.Add(dr);
}
As a side note, when working with WPF its easier to work with an ObservableCollection of objects instead of DataTables and DataRows. The data is much easier to understand and work with instead of trying to use DataRows and DataColumns.
var data = new ObservableCollection<MyObject>();
for (int i = 0; i < rowNumber; i++)
data.Add(new MyObject() { Wi = 0, Hi = 0 });
dataGrid1.ItemsSource = data;
I guess it´s better to use
Int32.TryParse(txtBexNumber.Text, out rownumber);
(maybe you get the error for parsing an empty string? If possible try to debug where exactly the null pointer exception accurs)
Also validate if
dr[rownumber] = dt.NewRow();
works as expected. If e.g. dr[10] doesn´t exist, you get an exception.

How to bind DataRow from DataSet?

I have a DataSet that contains values returned by my WCF function, as below:
DataSet ds = IserviceClient.FunctionMe(dcClient, dcBook);
#region show to dgv_item
DataTable dt1 = ds.Tables[0];
DataRow dr1;
int rc = dt1.Rows.Count;
clsMessageBoxes.ShowInfo(rc.ToString()); // Here show the number of row, so far the row is show the right value
//from here how to pass the row in that datatable to my datagridview? i
for (int i = 0; i < rc; i++)
{
dr1 = dt1.Rows[i];
dgv_item.Rows.Add();
dgv_item.Rows[i].Cells["dgvc_no"].Value = dr1["cnNo"].ToString();
dgv_item.Rows[i].Cells["dgvc_ID"].Value = dr1["cnID"].ToString();
dgv_item.Rows[i].Cells["dgvc_Name"].Value = dr1["cnName"].ToString();
}
Using this, I get the error
Rows cannot be programmatically added to datagridview's rows collection when the control is data-bound
Why do I get this, and how can I resolve it?

Add new row in a datagridview

I want to add new row to a gridview. The value of each cell comes from the bottom blank cells.
How to grab the cell's value?
private void AddRow_Click(object sender, EventArgs e)
{
DataTable dt = ds.Tables["test"];
DataRow dr = dt.NewRow();
// not sure how to add
}
Please also notice there is a checkbox in a column.
Thanks.
The code can be used
//define datatable (with all columns you want to have)...
DataRow dr;
int lastRow = this.dataGridView1.Rows.Count-2;
for(int i=0;i<dataGridView1.Columns.Count; i++)
{
// grab the values from the last row cells.
dr[i] = dataGridView1[i, lastRow].Value;
}
dataTable.Rows.Add(dr);
Self reslove it.
DataTable myTable = new DataTable();
DataColumn values = new DataColumn("Test");
myTable.Columns.Add(values);
DataRow rd = myTable.NewRow();
rd["Test"] ="";
myTable.Rows.Add(rd);
datagridview1.DataSource = myTable;
You can add Rows to the datatable by using the Add method.
dt.Rows.Add(dr);
You can access the values by using the overloaded brackets.
dt.Rows[a][b]
will give you the value at a certain row and certain column
To get the value of the Checkbox you can cast the object retrieved from dt.Rows[a][b] to a checkbox and then get the value of the Checked attribute.
((CheckBox)dt.Rows[a][b]).Checked
Hope this helps,
Matt

Cannot add new Row to Datatable

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);

How can I export a GridView.DataSource to a datatable or dataset?

How can I export GridView.DataSource to datatable or dataset?
Assuming your DataSource is of type DataTable, you can just do this:
myGridView.DataSource as DataTable
You should convert first DataSource in BindingSource, look example
BindingSource bs = (BindingSource)dgrid.DataSource; // Se convierte el DataSource
DataTable tCxC = (DataTable) bs.DataSource;
With the data of tCxC you can do anything.
Personally I would go with:
DataTable tbl = Gridview1.DataSource as DataTable;
This would allow you to test for null as this results in either DataTable object or null. Casting it as a DataTable using (DataTable)Gridview1.DataSource would cause a crashing error in case the DataSource is actually a DataSet or even some kind of collection.
Supporting Documentation: MSDN Documentation on "as"
Ambu,
I was having the same issue as you, and this is the code I used to figure it out. Although, I don't use the footer row section for my purposes, I did include it in this code.
DataTable dt = new DataTable();
// add the columns to the datatable
if (GridView1.HeaderRow != null)
{
for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++)
{
dt.Columns.Add(GridView1.HeaderRow.Cells[i].Text);
}
}
// add each of the data rows to the table
foreach (GridViewRow row in GridView1.Rows)
{
DataRow dr;
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text.Replace(" ","");
}
dt.Rows.Add(dr);
}
// add the footer row to the table
if (GridView1.FooterRow != null)
{
DataRow dr;
dr = dt.NewRow();
for (int i = 0; i < GridView1.FooterRow.Cells.Count; i++)
{
dr[i] = GridView1.FooterRow.Cells[i].Text.Replace(" ","");
}
dt.Rows.Add(dr);
}
If you do gridview.bind() at:
if(!IsPostBack)
{
//your gridview bind code here...
}
Then you can use DataTable dt = Gridview1.DataSource as DataTable; in function to retrieve datatable.
But I bind the datatable to gridview when i click button, and recording to Microsoft document:
HTTP is a stateless protocol. This means that a Web server treats each
HTTP request for a page as an independent request. The server retains
no knowledge of variable values that were used during previous
requests.
If you have same condition, then i will recommend you to use Session to persist the value.
Session["oldData"]=Gridview1.DataSource;
After that you can recall the value when the page postback again.
DataTable dt=(DataTable)Session["oldData"];
References:
https://msdn.microsoft.com/en-us/library/ms178581(v=vs.110).aspx#Anchor_0
https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/
I have used below line of code and it works, Try this
DataTable dt = dataSource.Tables[0];
This comes in late but was quite helpful. I am Just posting for future reference
DataTable dt = new DataTable();
Data.DataView dv = default(Data.DataView);
dv = (Data.DataView)ds.Select(DataSourceSelectArguments.Empty);
dt = dv.ToTable();

Categories

Resources