I am creating a website where our customers can order parts directly from us. I have a datatable setup and when users click a button it adds a quick detail of the order to a gridview. In the gridview, I have the edit and delete buttons enabled. the delete function works fine, however when you try to edit the information, it doesn't change the gridview with the new info. Here's what I have so far:
protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = (DataTable)Session["table"];
foreach (DataRow dr in dt.Rows)
{
part = Convert.ToString(dr["Part"]);
dr["Part"] = part;
dr["Quantity"] = qty;
dr["Ship-To"] = shipto;
}
griditems.EditIndex = -1;
BindData();
}
when trying this, it displays the gridview back with the original input values. I have also tried this (not working and get an error that says "There is no row at position 0":
DataTable dt = (DataTable)Session["table"];
GridViewRow row = griditems.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Ship-To"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
griditems.EditIndex = -1;
BindData();
Am I missing an EditItemTemplate in the aspx file, or am I just doing the RowUpdating all wrong?
You probably need to step back a bit and first check out how you could do create, update, delete and read with a grid view. Also, you may wanna check this post.
Related
I want to handle Checked event of CheckBox columns in my DataGridView and perform an operation based on column checked value (true/false). I tried to use CellDirtyStateChanged without any success. In fact I want to detect checked change immediately after the user checks or unchecks the check box.
Here is a description about my application. I am new to c# and making a "book my room" app for a place which provides guest housing to travelers. This screen may explain well what I wish to achieve;
This is a .GIF of a software which calculates hourly pay of an employee and this photo is an illustration of what actually I want to build:
Code for displaying my table in DataGridView is:
OleDbConnection connection = new OleDbConnection();
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select id,cusid,cusname,timein,
timeout,duration,amount,remark from entry";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
I added the checkbox column using this;
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "logout";
checkColumn.HeaderText = "Logout";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10;
dataGridView1.Columns.Add(checkColumn);
Whenever a user logs in from the login screen a new row will be inserted in the table and hence the dgv will be updated, with corresponding users entry.
I don't understand how to link those checkboxes with datagridview I tried celldirtystatechanged but nothing works, what would be the right way to associate the row with checkbox.
You can handle CellContentClick event of your DataGridView and put the logic for changing those cells there.
The key point is using CommitEdit(DataGridViewDataErrorContexts.Commit) to commits changes in the current cell to the data cache without ending edit mode. This way when you check for value of cell in this event, it returns current checked or unchecked value which you see in the cell currently after click:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//We make DataGridCheckBoxColumn commit changes with single click
//use index of logout column
if(e.ColumnIndex == 4 && e.RowIndex>=0)
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
//Check the value of cell
if((bool)this.dataGridView1.CurrentCell.Value == true)
{
//Use index of TimeOut column
this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DateTime.Now;
//Set other columns values
}
else
{
//Use index of TimeOut column
this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DBNull.Value;
//Set other columns values
}
}
On the first column is the id and want to select it to be redirected to a page that has more details that row.
He goes good if datasource is not changed but if it is modified or receive ID of the initial data, or gives me error for that initially did not have many rows.
My code is:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.DataBind();
GridViewRow row = GridView1.SelectedRow;
string strCell = row.Cells[1].Text;
string myPageUrl = "Meci.aspx?ID=" + strCell;
Response.Redirect(myPageUrl);
}
Please tell me how can I change it to work well!
I want to handle Checked event of CheckBox columns in my DataGridView and perform an operation based on column checked value (true/false). I tried to use CellDirtyStateChanged without any success. In fact I want to detect checked change immediately after the user checks or unchecks the check box.
Here is a description about my application. I am new to c# and making a "book my room" app for a place which provides guest housing to travelers. This screen may explain well what I wish to achieve;
This is a .GIF of a software which calculates hourly pay of an employee and this photo is an illustration of what actually I want to build:
Code for displaying my table in DataGridView is:
OleDbConnection connection = new OleDbConnection();
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select id,cusid,cusname,timein,
timeout,duration,amount,remark from entry";
command.CommandText = query;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
I added the checkbox column using this;
DataGridViewCheckBoxColumn checkColumn = new DataGridViewCheckBoxColumn();
checkColumn.Name = "logout";
checkColumn.HeaderText = "Logout";
checkColumn.Width = 50;
checkColumn.ReadOnly = false;
checkColumn.FillWeight = 10;
dataGridView1.Columns.Add(checkColumn);
Whenever a user logs in from the login screen a new row will be inserted in the table and hence the dgv will be updated, with corresponding users entry.
I don't understand how to link those checkboxes with datagridview I tried celldirtystatechanged but nothing works, what would be the right way to associate the row with checkbox.
You can handle CellContentClick event of your DataGridView and put the logic for changing those cells there.
The key point is using CommitEdit(DataGridViewDataErrorContexts.Commit) to commits changes in the current cell to the data cache without ending edit mode. This way when you check for value of cell in this event, it returns current checked or unchecked value which you see in the cell currently after click:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//We make DataGridCheckBoxColumn commit changes with single click
//use index of logout column
if(e.ColumnIndex == 4 && e.RowIndex>=0)
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
//Check the value of cell
if((bool)this.dataGridView1.CurrentCell.Value == true)
{
//Use index of TimeOut column
this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DateTime.Now;
//Set other columns values
}
else
{
//Use index of TimeOut column
this.dataGridView1.Rows[e.RowIndex].Cells[3].Value = DBNull.Value;
//Set other columns values
}
}
I have a WinForms application that has some data in a DataGridView. If a user double clicks on a cell then that row needs to be removed, and that works fine unless they click on a column first to sort the data by that column. If it has been sorted then the row index of the event still refers to the origional value in that row (I.E. Double clicking on the first item after sorting the data will remove the row that was at the top before the sort).
Is there a way for me to update the row indexes when the sort occurs?
Current initialize is similar to:
DataTable dt = new DataTable();
NpgsqlDataAdapter da = new NpgsqlDataAdapter(query, connection);
da.Fill(dt);
this.dgv1.DataSource = dt;
And the double click event is similar to:
private void dgv1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
DataTable dt = (DataTable)dgv1.DataSource;
if (e.RowIndex > -1)
{
dt.Rows.RemoveAt(e.RowIndex);
}
}
I suspect the issue is something very simple that I am not seeing right now..
You can use the property DataBoundItem of a DataGridViewRow to reference to the underlying DataRow and use the Remove method instead:
private void dgv1_CellDoubleClick(object sender, DataGridViewCellEventArgs e){
DataTable dt = (DataTable)dgv1.DataSource;
if(e.RowIndex>-1)
dt.Rows.Remove(((DataRowView)dgv1.Rows[e.RowIndex].DataBoundItem).Row);
}
I am new in ASP.NET ,I would have editable gridview in asp.net using C# , I found this editable gridview (Database , Project) in codeproject but i didn't realize how
its work specially this part of code:`
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
DropDownList dp= (DropDownList )e.Row .FindControl ("DropDownList1");
DataTable dt = load_department();
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem lt = new ListItem();
lt.Text = dt.Rows[i][0].ToString();
dp.Items.Add(lt);
}
dp.SelectedValue = drv[3].ToString();
RadioButtonList rbtnl = (RadioButtonList)e.Row.FindControl("RadioButtonList1");
rbtnl.SelectedValue = drv[5].ToString();
CheckBoxList chkb = (CheckBoxList)e.Row.FindControl("CheckBoxList2");
chkb.SelectedValue = drv[6].ToString();
}
}
}
`
Why she/he do this??
RowDataBound event fires when you bind the grid to a datasource, say, for example, a datatable.
For each row in the datatable, this code will run, and will, depending on the values in that row, put a value in each column of the grid view.
FindControl is used to find the control with the spcific name on that line of the grid view - remember, it will be repeated many times for as many rows as you have.
Once the control has been found, the value is set.
You are effectively setting up each row of the grid view for each row of the data in your data source.
Take a look at http://msdn.microsoft.com/en-us/magazine/cc163933.aspx for an overview of the intent behind this control.
** RowDataBound Occurs when a data row is bound to data in a GridView control.
**DataControlRowState Specifies the state of a row in a data control for eg.Edit,Insert,Selected etc
** RowState Gets the current state of the row with regard to its relationship to the DataRowCollection.
now in that if condition your dropdownbox (DropDownList1) is filled and RadioButton and Checkebox are setting up their selected values.