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
Related
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.
I have a datagridview that filled by a DataTable's Data; I want to get DataGridView's current selected row and pass this row as a DataRow to another form in dataGridView1_CellDoubleClick Event of my DataGridView.
I tried this code:
int rw = dataGridView1.CurrentRow.Index
DataRow PassingSessionInfo;
PassingSessionInfo = SessionsData.NewRow();
PassingSessionInfo = dataGridView1.Rows[rw];
and SessionsInfo is a DataTable.
I Got error, could you please help me?
Try this code:
int rw = dataGridView1.CurrentRow.Index
DataRow PassingSessionInfo;
PassingSessionInfo = ((dataGridView1.DataSource) as DataTable).Rows[rw];
PLease Try
int rw = dataGridView1.CurrentRow.Index
DataRow PassingSessionInfo;
PassingSessionInfo = SessionsData.NewRow();
DataTable dt = (DataTable)dataGridView1.DataSource;
dt.Rows.Add(dataGridView1.Rows[rw]);
PassingSessionInfo = dt.Rows[0];
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
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;
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();