Datagrid with Checkbox - asp.net

suppose consider in datagrid i have two text box when i fill records to it then i ill press tab key then another row automatically generated so how can i delete multiple rows using checkbox in datagrid not in gridview

int i = (DataGridView1.Rows.Count - 1); while ((i >= 0)) { i = (i -
1); foreach (DataGridViewRow dr in DataGridView1.Rows) { if
(dr.Cells("check").Value == false) {
DataGridView1.Rows.Remove(dr); } } }

Related

How to select only first 100 rows on Gridview

I have gridview that contains check boxes in one of the columns, then there is a "Select ALl" button which when clicked has to check top 100 CBs on the list, the client specifically stated they do not want pagination, it much easier to do this with pagination and display only 100 records per page then when the select all button is clicked everything on the given page gets selected however this is not what the client wants
Here is my code:
foreach (GridViewRow row in dgridTransactions.Rows)
{
for (int x = 0; x <=100;x++ )
{
var oneTransaction = (CheckBox)row.FindControl("chkAssigned");
oneTransaction.Checked = true;
}
}
If you want to run first hundred rows you only need this loop
for(int x = 0; x < 100; x++)
{
GridViewRow row = dgridTransactions.Rows[x];
// then manage row properties
CheckBox cb = (CheckBox)row.FindControl("chkAssigned");
cb.Checked = true;
}
using RowIndex you can keep track of row number.
foreach (GridViewRow row in dgridTransactions.Rows)
{
if(row.RowIndex<100 )
{
var oneTransaction = (CheckBox)row.FindControl("chkAssigned");
oneTransaction.Checked = true;
}
else
break;
}
There is issue in your code
You can use the below code
int x=0;
foreach (GridViewRow row in dgridTransactions.Rows)
{
if(x<100 )
{
var oneTransaction = (CheckBox)row.FindControl("chkAssigned");
oneTransaction.Checked = true;
}
else
break;
x++;
}
The foreach (GridViewRow row in dgridTransactions.Rows) loop runs for each row in your grid.
and in that you are using for (int x = 0; x <=100;x++ ){ which runs 100 times for every row.
You can use jquery or javascript for this Here is a JSFiddle which can help you
How about:
foreach (GridViewRow row in dgridTransactions.Rows.Cast<GridViewRow>().Take(100)) {
CheckBox cb = row.FindControl("chkAssigned") as CheckBox;
if (cb != null)
cb.Checked = true;
}
This will give the first items up to 100, so if you only have 90, it will give 90.
The diferent way of casting will also give you an additional security measure in case it fails to find the control. A direct cast will just throw an exception, which is always heavier then checking if the castes object is not null...
If you dont care about the cast verification, you can just inline everything into this:
dgridTransactions.Rows.Cast<GridViewRow>().Take(100).ToList().ForEach(x => ((CheckBox)x.FindControl()).Checked = true);

DevExpress ASPxComboBox Items Color Change from Code Behind

I have a ASPxComboBox for which I'm binding data based on 2 conditions.
Now,I need to show Color for items in combobox based on condition.
My Code :
var dataMainBranchUsers = (from xx in VDC.SURVEY_USER_DETAILS
where xx.BRANCH_ID == 1 && (xx.USER_LEVEL == 2 || xx.USER_LEVEL == 5)
select new
{
xx.USER_NAME,
xx.USER_ID,
xx.USER_LEVEL
}).ToList();
DataTable dtMainBranchUsers = LINQToDataTable(dataMainBranchUsers);
for (int i = 0; i < dtMainBranchUsers.Rows.Count; i++)
{
string strlevel = dtMainBranchUsers.Rows[i]["USER_LEVEL"].ToString();
string struser = dtMainBranchUsers.Rows[i]["USER_NAME"].ToString();
if (strlevel == "2")
{
dtMainBranchUsers.Rows[i]["USER_NAME"] = struser + " - Admin";
}
else
{
dtMainBranchUsers.Rows[i]["USER_NAME"] = struser + " - Survey User";
}
}
Cmb_UserName.TextField = "USER_NAME";
Cmb_UserName.ValueField = "USER_ID";
Cmb_UserName.DataSource = dtMainBranchUsers;
Cmb_UserName.DataBind();
Now, I need to differentiate based on USER_LEVEL and show colors.
Is this possible?
From DevExpress
I'm afraid, the ASPxListBox (which is the part of the ASPxComboBox) doesn't allow to set specific color for each item.
I suggest you to use the ASPxDropDownEdit. This control allows to put anything in its DropDownWindowTemplateContainer.
For example, you can put the ASPxGridView and set color for each row using the HtmlRowPrepared event handler.
See here.

Gridview remove items

I have I gridview in which the data source is a List<T>. When I try to remove an item from the gridview in my buttonRemove_Click() function another function which handles the RowDeleting event is invoked where I remove the item from the List<T> as well. The problem is that if I select to remove multiple items from the gridview the index of the gridview and that of my List<T> un-syncs. For example I have 10 items in my gridview and in my List and I try to remove the last two items. Here is how I do it in my buttonRemove_Click function
foreach (GridViewRow row in gridViewItems.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("checkBox");
if (cb != null && cb.Checked)
{
gridViewItems.DeleteRow(row.DataItemIndex);
}
}
Then in the RowDeleting function, I'll first receive the event for the index 8, I removed it. Now when it comes to deleting the last item (index 9), then it'll throw exception because the index is out of range. How do I solve this problem?
I think the problem will be solved if I try removing the rows in reverse order i.e. starting from the highest index. Can anyone tell how can this be done?
GVGLCode1.DataSource = dt;
GVGLCode1.DataBind();
int iCount = GVGLCode1.Rows.Count;
for (int i = 0; i <= iCount; i++)
{
CheckBox cb = (CheckBox)GVGLCode1.rows[i].FindControl("checkBox");
if (cb != null && cb.Checked)
{
GVGLCode1.DeleteRow(i);
}
}
Please try with this.
May be it can help u.

Get the total of complete column of gridview in asp.net

I am Using asp.net grid view and i have one column in that namely Calories and i want to show whole caloreis total at the top. i used the following code:
int count = grdViewSample.Rows.Count;
double totCalories = 0;
Label lblCalories = new Label();
foreach (GridViewRow row in grdViewSample.Rows)
{
lblCalories = (Label)row.FindControl("lblCalories");
totCalories = totCalories + Convert.ToDouble(lblCalories.Text);
}
lblTotalCaloreis.Text = lblTotalCaloreis.Text + " " + totCalories;
and my problem is that the calories total get changed by paging and i want total caloreies available in grid view.
Thanks
Instated of using
grid view row property retrive no of rows from the data set directly because row property changes with the paging
foreach (DataRow row in dsFood.Tables[0].Rows)
{
totCal += Convert.ToDouble(row["calories"]);
}
lblCalories.Text = " " + totCal;

asp .net dropdownlist trim data

In the asp.net dropdownlist i need to trim the data inside the list. e.g if my drop down has 10 records in it and i only want to show the first 20 chars of each record, then how do i do it?. Also if records are only 10 chars then from 20 chars the dropdownlist should automatically resize to 10 chars. any ideas?
If you can't trim the data at the source (i.e. the database query or wherever you're getting the data from), then you can just modify the data after the dropdown has been databound.
myDropDown.DataBind();
foreach (var item in myDropDown.Items)
{
if (item.Text.Length > 20)
{
item.Text = item.Text.Substring(0, 10);
}
}
I can't recall if the ASP.NET version has a Tag property but if it does this would shorten the text and preserve the original value (copied original from womp):
myDropDown.DataBind();
foreach (var item in myDropDown.Items)
{
if (item.Text.Length > 20)
{
item.Tag = item.Text;
item.Text = item.Text.Substring(0, 10);
}
}
If not then maybe Attributes (forgive me if my syntax is off, no compiler here to verify against):
myDropDown.DataBind();
foreach (var item in myDropDown.Items)
{
if (item.Text.Length > 20)
{
item.Attributes["title"] = item.Text;
item.Text = item.Text.Substring(0, 10);
}
}

Resources