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".
Related
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.
Now i have one cell i added it in the designer but if i want to add more cells in my code ?
I tried in the constructor to do:
DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[0].Clone();
row.Cells[0].Value = "XYZ";
row.Cells[1].Value = 50.2;
dataGridView1.Rows.Add(row);
But getting exception on:
row.Cells[1].Value = 50.2;
Index was out of range. Must be non-negative and less than the size of the collection
Yes you should get an exception. Look at your code below. With row.Cells[1] you are actually referring to 2nd column in your gridview which apparently doesn't exists and so the end result.
row.Cells[1].Value = 50.2;
If you want to have another column to your grid then either add that using your designer surface (OR) create a DataTable with required columns and set that to DataSource of your gridview.
How to create a DataTable and attach it to GridView?
Search Google
EDIT:
In case you are still struggling understanding my comment then this is what I meant to say (sample code)
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("Message Subject");
dt.Columns.Add(dc);
DataRow dr = dt.NewRow();
dr[dc] = "test";
DataRow dr1 = dt.NewRow();
dr1[dc] = "test123";
dt.Rows.Add(dr);
dt.Rows.Add(dr1);
this.dataGridView1.DataSource = dt;
}
I am generating a DataTable in C# and I need to disable sorting the columns via code. The code is something like:
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("File_Name");
dt.Columns.Add("Create_Date");
dt.Columns.Add("Status");
dr = dt.NewRow();
dataGridView1.DataSource = dt;
How can this be accomplished? I did check for things like dt.SortMode = <something disabled> but so far didn't find any SortOrder on the data element.
Here's a more robust example. I tested this in Windows forms application (.NET 4.5, VS 2012)
DataTable dt = new DataTable();
dt.Columns.Add("File_Name");
dt.Columns.Add("Create_Date");
dt.Columns.Add("Status");
DataRow dr = dt.NewRow();
dr["File_Name"] = "abc.txt";
dr["Create_Date"] = DateTime.Now;
dr["Status"] = "Pending";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["File_Name"] = "xyz.bmp";
dr["Create_Date"] = DateTime.Now;
dr["Status"] = "Complete";
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.SortMode = DataGridViewColumnSortMode.NotSortable;
}
Hope this helps.
:) David
"AllowUserToOrderColumns" has nothing to do with sorting data, it determines whether or not the user is allowed to rearrange the columns in the grid. You can disable sorting certain grid columns by setting the respective columns' SortMode as such
myDataGridViewTextBoxColumn.SortMode = DataGridViewColumnSortMode.NotSortable
I have on a usercontrol a datagridview. I created a datatable and I set the source of datagrid to be this datatable. I want,at runtime,to be able to add how many rows on gridview I want at every button click.
My code :
private DataTable CreateTable()
{
Datatable table=new Datatable();
table.Columns.Add("Name".ToString());
table.Columns.Add("Size".ToString());
DataRow dr = table.NewRow();
dr["Name"] = "Mike";
DataRow dr2 = table.NewRow();
dr2["Name"] = "Ryan;
DataRow dr3 = table.NewRow();
dr3["Name"] = "Taylor";
dr["Size"] = " one";
dr2["Size"] = "two";
table.Rows.Add(dr);
table.Rows.Add(dr2);
table.Rows.Add(dr3);
return table;
//and on my constructor I set gridview.DataSource=Datatable;
}
//Code on the event:
private void button_Click(object sender, EventArgs e)
{
DataRow NewRow = table.NewRow();
table.Rows.Add(NewRow);
}
You need to define the DataTable at form level. Then in button click you can do:
private void button_Click(object sender, EventArgs e)
{
DataRow NewRow = table.NewRow();
table.Rows.Add(NewRow);
gridview.DataSource=table; //specify the source
}
For defining table at form level:
DataTable table; //DataTable at form level
private DataTable CreateTable()
{
table=new Datatable(); //here insntianting the form level table.
table.Columns.Add("Name".ToString());
table.Columns.Add("Size".ToString());
DataRow dr = table.NewRow();
dr["Name"] = "Mike";
DataRow dr2 = table.NewRow();
dr2["Name"] = "Ryan;
DataRow dr3 = table.NewRow();
dr3["Name"] = "Taylor";
dr["Size"] = " one";
dr2["Size"] = "two";
table.Rows.Add(dr);
table.Rows.Add(dr2);
table.Rows.Add(dr3);
return table;
//and on my constructor I set gridview.DataSource=Datatable;
}
I would recommend the following approach for better handling.
Create generic list, for every click append the list with the new set of data, then convert the list to DataTable as said in the below link, then bind DataTable to the grid.
Convert generic List/Enumerable to DataTable?
If you want sample code please let me know.
I want to get a DataTable from DataGridView of the Grid values.
In other words DataTable same as DataGridView Values
Might be a nicer way to do it but otherwise it would be fairly trivial to just loop through the DGV and create the DataTable manually.
Something like this might work:
DataTable dt = new DataTable();
foreach(DataGridViewColumn col in dgv.Columns)
{
dt.Columns.Add(col.Name);
}
foreach(DataGridViewRow row in dgv.Rows)
{
DataRow dRow = dt.NewRow();
foreach(DataGridViewCell cell in row.Cells)
{
dRow[cell.ColumnIndex] = cell.Value;
}
dt.Rows.Add(dRow);
}
You can cast the DataSource object from the DataGridView to a DataTable
DataTable dt = new DataTable();
dt = (DataTable)dataGridView1.DataSource;
you can use the following code also, this code fill not effect on your DataGridView when you do some add or delete rows in the datatable
DataTable dt = new DataTable();
dt = Ctype(dataGridView1.DataSource,DataTable).copy();
You can try as follow:
using System.Data;
using System.Windows.Forms;
namespace BLL
{
public class Class_DataGridview_To_DataTable
{
public static DataTable UDF_Convert_DataGridView_To_Datatable(DataGridView ImpGrd)
{
DataTable ExportDataTable = new DataTable();
foreach (DataGridViewColumn col in ImpGrd.Columns)
{
ExportDataTable.Columns.Add(col.Name);
}
foreach (DataGridViewRow row in ImpGrd.Rows)
{
DataRow dRow = ExportDataTable.NewRow();
foreach (DataGridViewCell cell in row.Cells)
{
dRow[cell.ColumnIndex] = cell.Value;
}
ExportDataTable.Rows.Add(dRow);
}
return ExportDataTable;
}
}
}