I'm using checkbox to remove a row from data table; when I check one of the boxes; the row is removed. But if i check all the boxes; it gives this error message There is no row at position 1.. And if I go back to checking one box no row is removed.
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.FindControl("chkInvoice") as CheckBox;
if (chk != null && chk.Checked)
{
dt.Rows.RemoveAt(row.RowIndex);
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
If you use foreach, you will be modifying the rows while iterating them. So do a reverse loop and remove based on index.
for(int i = GridView1.Rows.Count - 1; i >= 0; i--)
{
var row = GridView1.Rows[i];
CheckBox chk = row.FindControl("chkInvoice") as CheckBox;
if (chk != null && chk.Checked)
{
dt.Rows.RemoveAt(i);
}
}
Related
I have DataGridView with a CheckBox Column(Header: Status) and Textbox Column(Header: Quantity). I want to check automatically the checkbox column if the quantity is 0.
Loop through the rows if the datagridview like this:
foreach (DataGridViewRow row in dataGridView1.Rows)
{
//Check Column 3 for quantity
if (row.Cells[3].Value.ToString() == "0")
{
//Get checkbox in column 1 and cast it to a checkbox
DataGridViewCheckBoxCell cell = row.Cells[1] as DataGridViewCheckBoxCell;
cell.Value = cell.TrueValue;
}
}
I have the following code:
int countRows = dataGridView3.SelectedRows.Count;
int rowIndex = 0;
foreach (DataGridViewRow row in dataGridView3.SelectedRows)
{
int selectedRowIndex = dataGridView3.SelectedCells[rowIndex].RowIndex;
DataGridViewRow selectedRow = dataGridView3.Rows[selectedRowIndex];
capacity = Convert.ToInt32(selectedRow.Cells["Cust_Number"].Value);
capStore.Add(capacity);
rowIndex++;
}
I try to go through each selected row in my DataGridView and store the value from the column "Cust_Number" into an ArrayList, so that I can change it later. Somehow he just grabs the second row each time I iterate and I have the same value in my ArrayList duplicated. What is wrong here?
I would try the following code :
if(dataGridView3.SelectedRows != null && dataGridView3.SelectedRows.Count > 0)
{
foreach (DataGridViewRow dgvr in dataGridView3.SelectedRows)
{
int tempVal = 0;
if(dgvr.Cells["Cust_Number"].Value != null && int.TryParse(dgvr.Cells["Cust_Number"].Value.ToString(), out tempVal))
{
capStore.Add(tempVal);
}
}
}
This is simpler and safer.
I have a loaded DataGrid. In that DataGrid the first column is a CheckBox, and the second column is "Name". Also I have saved one field "Name" in the database . Here I want to make the CheckBox to be checked if the Name is equal to the data which I stored in the database.
Here my problem is that ,I am getting only one CheckBox to to be checked.
ex:[ If my expected result is 1st, 2nd and 3dr CheckBoxes to be checked , But I am getting only the 3rd one as checked. ]
my sample code is
foreach (GridViewRow row in GrdProduct.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lblproduct = (Label)row.FindControl("lblProduct");
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelectAll");
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
DataRow r = dt.Rows[rowIndex];
if (Convert.ToString(r["productName"]) == lblproduct.Text)
{
chkSelect.Checked = true;
}
else
{
chkSelect.Checked = false;
}
}
}
Finally I got the solution ... Simply removed the else part
else
{
chkSelect.Checked = false; }
--
thank you all...
Sorry I am blind. You are looping through the datarows and of course you most likely have only one match. In all other cases it becomes unchecked right after. Use the following:
foreach (GridViewRow row in GrdProduct.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label lblproduct = (Label)row.FindControl("lblProduct");
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelectAll");
chkSelect.Checked = false;
for (int rowIndex = 0; rowIndex < dt.Rows.Count || !chkSelect.Checked; rowIndex++)
{
DataRow r = dt.Rows[rowIndex];
if (Convert.ToString(r["productName"]) == lblproduct.Text)
{
chkSelect.Checked = true;
}
}
}
Als when comparing strings I recommend http://msdn.microsoft.com/en-us/library/system.string.equals(v=vs.110).aspx
COmpared what you are going to do with the data and controls in the grid I also recommend additional validation.
I have a GridView that has 10 columns populated by CheckBoxes. But instead of using FindControl() is there a way to get the CheckBox.Checked value by using a loop?
Current Code:
if (e.CommandName == "updaterow")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
// TableCell BranchCode = selectedRow.Cells[0];
CheckBox cb101 = (CheckBox)selectedRow.FindControl("cb101");
CheckBox cb102 = (CheckBox)selectedRow.FindControl("cb102");
//...and so on
}
ASPX CODE:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="101">
<ItemTemplate>
<asp:CheckBox runat="server" id="cb101" AutoPostBack="false" Checked='<%# Eval("101").ToString()=="1" ? true : false %>' Enabled='<%#(String.IsNullOrEmpty(Eval("101").ToString()) ? false: true) %>'/>
</ItemTemplate>
</asp:TemplateField>
....and so on
<asp:ButtonField ButtonType="Button" CommandName="updaterow" Text="Update"/>
</Columns>
</asp:GridView>
Try this,
Using foreach Loop:
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chk = row.Cells[0].Controls[0] as CheckBox;
if (chk != null && chk.Checked)
{
// ...
}
}
Use it in OnRowCommand event and get checked CheckBox value.
GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);
int requisitionId = Convert.ToInt32(e.CommandArgument);
CheckBox cbox = (CheckBox)row.Cells[3].Controls[0];
For run all lines of GridView don't use for loop, use foreach loop like:
foreach (GridViewRow row in yourGridName.Rows) //Running all lines of grid
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("chkRow") as CheckBox);
if (chkRow.Checked)
{
//if checked do something
}
}
}
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox chkbox = (CheckBox)row.FindControl("CheckBox1");
if (chkbox.Checked == true)
{
// Your Code
}
}
If you want a method other than findcontrol try the following:
GridViewRow row = Gridview1.SelectedRow;
int CustomerId = int.parse(row.Cells[0].Text);// to get the column value
CheckBox checkbox1= row.Cells[0].Controls[0] as CheckBox; // you can access the controls like this
foreach (DataRow row in DataRow row in GridView1.Rows)
{
foreach (DataColumn c in GridView1.Columns)
bool ckbVal = (bool)(row[c.ColumnName]);
}
Blockquote
foreach (GridViewRow row in tempGrid.Rows)
{
dt.Rows.Add();
for (int i = 0; i < row.Controls.Count; i++)
{
Control control = row.Controls[i];
if (control.Controls.Count==1)
{
CheckBox chk = row.Cells[i].Controls[0] as CheckBox;
if (chk != null && chk.Checked)
{
dt.Rows[dt.Rows.Count - 1][i] = "True";
}
else
dt.Rows[dt.Rows.Count - 1][i] = "False";
}
else
dt.Rows[dt.Rows.Count - 1][i] = row.Cells[i].Text.Replace(" ", "");
}
}
You want an independent for loop for all the rows in grid view, then refer the below link
http://nikhilsreeni.wordpress.com/asp-net/checkbox/
Select all checkbox in Gridview
CheckBox cb = default(CheckBox);
for (int i = 0; i <= grdforumcomments.Rows.Count – 1; i++)
{
cb = (CheckBox)grdforumcomments.Rows[i].Cells[0].FindControl(“cbSel”);
cb.Checked = ((CheckBox)sender).Checked;
}
Select checked rows to a dataset; For gridview multiple edit
CheckBox cb = default(CheckBox);
foreach (GridViewRow row in grdforumcomments.Rows)
{
cb = (CheckBox)row.FindControl("cbsel");
if (cb.Checked)
{
drArticleCommentsUpdates = dtArticleCommentsUpdates.NewRow();
drArticleCommentsUpdates["Id"] = dgItem.Cells[0].Text;
drArticleCommentsUpdates["Date"] = System.DateTime.Now;dtArticleCommentsUpdates.Rows.Add(drArticleCommentsUpdates);
}
}
I have a simple function in my program that selects the current cell, then it (FillsDown) copies the current cell into ALL the cells below it.
However my simple function places the value in the Row at the bottom which is the new row and I do not want the value to be populated here.
Heres the current code. (Where COLINDEX is just the column im referring to in my grid)
string replacestring = row.Cells[COLINDEX].Value.ToString();
foreach (DataGridViewRow row in dataGrid1.Rows)
{
if (row.Index > startrow)
{
row.Cells[COLINDEX].Value = replacestring;
}
}
Is there a simeple method/property I can check against so I dont accidently populate the last row ?
My example below uses a fake property (.Exist)
foreach (DataGridViewRow row in dataGrid1.Rows)
{
if ((row.Index > startrow) && row.Exist)
{
row.Cells[COLINDEX].Value = replacestring;
}
}
DataGridViewRow has a property called IsNewRow
And try to walk the Rows collection using the index
for(int x = startRow + 1; x < dataGrid1.Rows.Count; x++)
{
DataGridViewRow row = dataGrid1.Rows[x];
if (row.IsNewRow == false)
{
row.Cells[COLINDEX].Value = replacestring;
}
}