How Can Convert DataRow to DataRowView in c# - c#

Can or how to convert DataRow to DataRowView?
For example:
DataTable dt=ds.Tables[0];
DataRow dr= dt.NewRow();
DataRowView drv = ????

Use
DataTable dt=ds.Tables[0];
DataRow dr= dt.NewRow();
DataRowView drv= dt.DefaultView[dt.Rows.IndexOf(dr)];

The above method does not work if the DataRow status is Detached.
This code snippet could be used in such case:
DataRow dRow = dataTable.NewRow();
//...
DataRowView dRowView = dRow.Table.DefaultView.AddNew();
for (int i = 0; i < dRow.ItemArray.Length; i++)
{
dRowView.Row[i] = dRow[i];
}

DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().FirstOrDefault(a => a.Row == desRow);
works, this is a litte shorter without "where" ;-)

Use.
It also works if the DataGrid is ordered.
DataRowView selecRow = dataTable.DefaultView.Cast<DataRowView>().Where(a => a.Row == desRow).FirstOrDefault();

Related

How can I set read only property for Particular 'dr' in DATAROW

I try to shown a particular row as a read only format for ListBox. But when getting value from database I can't able to set the read only property for the particular row.
C#:
DataTable dt = new DataTable();
dt.Columns.Add("SectionId", typeof(string));
dt.Columns.Add("RegionalLanguage1", typeof(string));
foreach(DataTable table in dsPopularPrograms.Tables)
{
foreach(DataRow dr in table.Rows)
{
var IsDisable = dr["IsPrabalamanavai"].ToString();
DataRow newRow = dt.NewRow();
newRow["SectionId"] = dr["SectionId"].ToString();
newRow["RegionalLanguage1"] = dr["RegionalLanguage1"].ToString();
if (IsDisable == "D")
{
//Need to set the row as readonly
}
dt.Rows.Add(newRow);
}
}
In the particular condition I want to set the read only property. I don't know how it is.
Please give some suggestion.

how to add some selected items and their indexes from listbox to datatable

I`m trying to use something like this:
I have 20 list items and I have to choose some of them, then add them to datatable in first column elements, in second - their indexes.
DataTable dt = new DataTable();
dt.Columns.Add("Фактор-Критерій", typeof(string));
dt.Columns.Add("Ранг", typeof(string));
ChoosenCriteria.DataSource = dt;
List<string> selectedItems = new List<string>();
foreach (string o in listBox1.SelectedItems)
selectedItems.Add(o);
List<int> selectedItemIndexes = new List<int>();
foreach (int o in listBox1.SelectedIndices)
selectedItemIndexes.Add(listBox1.Items.IndexOf(o));
DataRow dr = dt.NewRow();
dr[0] = selectedItems.ToString();
dr[1] = selectedItemIndexes.ToString();
dt.Rows.Add(dr););
I can not do this, all i have at the end is on screen:enter image description here
Move the DataSource binding statement after you have done with the modification like
for(int i=0; i<selectedItems.Count;i++
{
DataRow dr = dt.NewRow();
dr[0] = selectedItems[i].ToString();
dr[1] = selectedItemIndexes[i].ToString();
dt.Rows.Add(dr);
}
ChoosenCriteria.DataSource = dt;

Why conversion from Gridview to datatable fails?

I have been trying to convert GRIDVIEW to DataTable but it doesn't give me data, it only gives me HEADERS of GridView but not the data inside it. Why ? I am using template fields in gridview
Calling it from event:
DataTable dt = GetDataTable(GridView DenominationsWiseTransactions);
Conversion function:
DataTable GetDataTable(GridView dtg)
{
DataTable dt = new DataTable();
// add the columns to the datatable
if (dtg.HeaderRow != null)
{
for (int i = 0; i < dtg.HeaderRow.Cells.Count; i++)
{
dt.Columns.Add(dtg.HeaderRow.Cells[i].Text);
}
}
// add each of the data rows to the table
foreach (GridViewRow row in dtg.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);
}
return dt;
}
So, when you are looping through the rows in the datagrid, it will include headers, the rows and the footer.
To see just the data rows you need to do something like:
if (e.Row.RowType == DataControlRowType.DataRow)
{ //convert that row }
Your conversion line might need some work, but at least you'll only need to concern yourself with datarows now.

How to convert DataGridViewRowCollection to DataRow[]

I want to convert DataGridViewRowCollection to DataRow[]
Is there any way like:
DataRow[] rowarray=(DataRow[])(datagridview.Rows);
Or do I have to convert each row using foreach?
You need to convert it to a DataTable first.
DataTable data = (DataTable)(datagridview.DataSource);
Then copy the rows into a DataRow array.
DataRow[] drArray = new DataRow[data.Rows.Count];
data.Rows.CopyTo(drArray, 0);
How about with Linq?
DataRow[] rows = dataGridView1.Rows.Cast<DataGridViewRow>()
.Select(r => r.DataBoundItem as DataRowView)
.Where(drv => drv != null)
.Select(drv => drv.Row)
.ToArray();
I hope this will help. Try it and let us know.
void covertDGVToRowColl()
{
DataRow[] rowColl = new DataRow[dgTemp.Rows.Count]; //dgTemp is the datagridview
DataTable dtTable = new DataTable();
foreach (DataGridViewColumn dgvColumn in dgTemp.Columns)
{
dtTable.Columns.Add(dgvColumn.Name);
}
DataRow newRow;
int i = 0;
foreach (DataGridViewRow dRow in dgTemp.Rows)
{
newRow = dtTable.NewRow();
foreach (DataGridViewColumn dgvColumn in dgTemp.Columns)
{
newRow[dgvColumn.Name] = dRow.Cells[dgvColumn.Name].Value;
}
rowColl[i] = newRow;
++i;
}
}

Remove a Datarow from a Datatable

I have a windows application in which I am going to delete a datarow from a datatable.
But I got an exception.
The given DataRow is not in the current DataRowCollection.
The code:
DataTable dt = new DataTable();
DataRowView currentDataRowView = (DataRowView)DataGridView1.CurrentRow.DataBoundItem;
DataRow row = currentDataRowView.Row;
dt.Rows.Remove(row); // exception here.
DataGridView1.DataSource = dt;
The datatable dt's information as the image shown.
I think that I already have casted datarowview to datarow.
EDIT:
dt was created from another DataGridView.
foreach (DataGridViewRow row in DatGridView2.Rows)
{
DataGridViewCheckBoxCell check = row.Cells[0] as DataGridViewCheckBoxCell;
if (check.Value != null)
{
if ((bool)check.Value)
{
//this row has a checkBox set to true (tick is added)
//add this row to dataTable ...
DataRow myRow = (row.DataBoundItem as DataRowView).Row;
DataRow dr = dt.NewRow();
dr[0] = myRow[0];
dr[1] = myRow[1];
dr[2] = myRow[2];
dr[3] = myRow[3];
if (!dt.Rows.Contains(dr[0]))
{
dt.Rows.Add(dr);
}
}
}
I don't think you can reference a different DataTable than the one where CurrentRow.DataBoundItem is coming from.
Your dt DataTable is being constructed from DataGridView2 but CurrentRow.DataBoundItem is coming from DataGridView1.
You will have to find the matching row in DataGridView1 yourself before you can delete it.
I have tried your code, and it's working with no problem:
DataTable dt = (DataTable)dataGridView1.DataSource;
DataRowView currentDataRowView = (DataRowView)dataGridView1.Rows[0].DataBoundItem;
DataRow row = currentDataRowView.Row;
dt.Rows.Remove(row);
dataGridView1.DataSource = dt;
So as i have wrote above, the error "The given DataRow is not in the current DataRowCollection" means that your trying to delete a row that is not in the DataTable "dt".

Categories