I have these checkboxlist each cell of a gridview. Now, I am trying to get the selected items each checkboxlist but it failed. Any help please! Thanks!
foreach (GridViewRow gvRow in gvReg.Rows)
{
for (int ctr = 0; ctr <= 4 - 1; ctr++)
{
if (ctr == 0)
{
szCheckBoxListName = "cblMultiSelect";
szRegionName = "lblRegionName";
}
else
{
szCheckBoxListName = "cblMultiSelect" + ctr;
szRegionName = "lblRegionName" + ctr;
}
cbl=(CheckBoxList)gvRow.Cells[ctr].FindControl(szCheckBoxListName);
if (cbl.Items.Count > 0)
{
foreach (ListItem li in cbl.Items)
{
if (li.Selected)
{
iItemCount = iItemCount + 1;
}
}
}
}
}
itemCount returns zero always even if I have selected several items on those checkboxlists.
Are you data binding on Page_Load method? If yes, you must do this:
if(!IsPostBack)
{
GridView1.DataSource = Your Datas;
}
Related
Please help me why my selected item not deleted from grid-view in c#.
Code:
rowindex = this.AB.ABProducts.Count - 1;
foreach (GridViewRow row in gc.GridRows)
{
CheckBox chkRow = row.Cells[2].FindControl("chkID") as CheckBox;
{
if (rowindex >= 0)
{
if (chkRow.Checked)
{
this.AB.ABProducts.Remove(rowindex);
}
}
}
rowindex--;
}
I want to hide empty row in one particular column. I tried to but negative. Below is my code:
protected void gvDb_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow rw in gvDb.Rows)
{
if ((string.IsNullOrEmpty(rw.Cells[1].Text) | (rw.Cells[1].Text == "")))
{
rw.Visible = false;
}
}
}
for (int i = 0; i < gvDb.RowCount - 1; i++)
{
var row = gvDb.Rows[i];
if (string.IsNullOrEmpty(Convert.ToString(row.Cells[1].Value)))
{
row.Visible = false;
}
}
This will work,
use for instead of foreach to iterate all the rows except last row which is empty.
i need to create checkboxlist which should select max 4 items.
if user select the 5th item then it should clear particular 5th item only.
currently this code always clearing the first item of checkboxlist.
this is my c#:
protected void lstSalesPerson_SelectedIndexChanged(object sender, EventArgs e)
{
var items = from ListItem li in lstSalesPerson.Items
where li.Selected == true
select li;
Label1.Text = "";
for (int i = 0; i < lstSalesPerson.Items.Count; i++)
{
if (lstSalesPerson.Items[i].Selected == true)
{
if (items.Count() > 4)
{
Label1.Text = "checked maximum 4 items.";
lstSalesPerson.Items[i].Selected = false;
}
}
}
}
this is my html:
<asp:ListBox ID="lstSalesPerson" runat="server" SelectionMode="Multiple" AutoPostBack="true" OnSelectedIndexChanged="lstSalesPerson_SelectedIndexChanged"> </asp:ListBox>
you should reverse the loop as follows. In your loop it started from index[0] and check if total selected item is >4 if correct it will false the first item.
So the loop should be from the reverse index. so if 5 item selected then the first index selected will be [4] and it will get false
try below code
for (int i = lstSalesPerson.Items.Count-1; i >=0 ; i--)
{
if (lstSalesPerson.Items[i].Selected == true)
{
if (items.Count() > 4)
{
Label1.Text = "checked maximum 4 items.";
lstSalesPerson.Items[i].Selected = false;
}
}
}
i found answer using javascript it is as follows:
This might help someone: i'm using this control .multiselect-container > li > a > label > input because my control was rendered using this way in templates design. but you can use the logic for your code.. Thanks
$('.multiselect-container > li > a > label > input').click(function (e) {
var ddl = document.getElementById("<%=lstSalesPerson.ClientID%>");
if ($(this).prop('checked')) {
var count = 0;
for (var i = 0; i < ddl.options.length; i++) {
if (ddl.options[i].selected) {
count++;
if (count > 3) {
$(this).removeAttr('checked');
$(this).parent().parent().parent().prop('checked', false);
$(this).parent().parent().parent().prop('clicked', false);
$(this).parent().parent().parent().removeClass('active');
alert("You can select Max 4 sales person.");
return false;
}
}
}
}
});
Solution, for at least a specific cell: GridView1.Rows[i].Cells[j].Text;
I've build a simple CSV-Fileupload. After the user uploaded the file he should be able to evaluate the data. When the fileupload was successful the data gets loaded into the GridView1, with this code: (Problem below the code)
string[] readCSV = File.ReadAllLines(lblFilePath.Text);
DataTable dt = new DataTable();
bool bSplitMe = false;
foreach (var rLine in readCSV)
{
if (bSplitMe)
{
string[] aSplittedLine = rLine.Split(";".ToCharArray());
try
{
dt.Rows.Add(aSplittedLine);
}
catch(System.Exception)
{
txtBoxFileOut.Text = rLine;
break;
}
}
else
{
if (rLine.ToLower().StartsWith("definedtestid;"))
{
bSplitMe = true;
string[] aSplittedLine = rLine.Split(";".ToCharArray());
foreach (var rCol in aSplittedLine)
{
dt.Columns.Add(rCol);
}
}
else
{
txtBoxFileOut.Text += rLine.ToString() + "\n";
}
}
}
dt.Columns.Remove("Column1");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
if (string.IsNullOrEmpty(dt.Rows[i][j].ToString()))
{
dt.Rows[i][j] = "0";
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
After this the user should be able to select a row and display the data from that row in a chart.
Problem: I'm not able to read data from the cells I want, or to read from a "hardcoded" cell.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
GridViewRow row = GridView1.SelectedRow;
txtOutputfield.Text = row.Cells[2].Text;
}
Please check your cell index. Is it correct? For example: the third column will have index "2" not "3"
And, if you use a control to store the data, you need to find that control:
txtOutputfield.Text =
row.Cells[2].FindControl('placeyourcontrolnamehere').Text;
For a specific Cell this worked fine
txtOutputfield.Text = GridView1.Rows[i].Cells[j].Text;
I'm trying to select values in a CheckBoxList control based on a data source. I have five items in the CheckBoxList and three items in the data source, but in the loop I only get one item selected.
if (ddlUserId.SelectedIndex != 0)
{
RoleDetails rd;
rd = CatalogAccess.GetSingleUserRole(ddlUserId.SelectedValue.ToString());
for (int i = 0; i < cblRoles.Items.Count; i++)
{
cblRoles.Items.FindByValue(rd.RoleID.ToString()).Selected = true;
}
}
I tried this, but it still selects only one item:
RoleDetails rd;
for (int i = 0; i < cblRoles.Items.Count; i++)
{
rd = CatalogAccess.GetSingleUserRole(ddlUserId.SelectedValue.ToString());
if (cblRoles.Items[i].Value == rd.RoleID.ToString())
cblRoles.Items[i].Selected = true;
}
CheckboxList bind code
cblRoles.DataSource = CatalogAccess.GetRoles();
cblRoles.DataTextField = "RoleDetails";
cblRoles.DataValueField = "RoleId";
cblRoles.DataBind();
When you use for loop you need to use index value (Here it is "i"), like
for (int i = 0; i < cblRoles.Items.Count; i++)
{
if(cblRoles.Items[i].Value == rd.RoleID.ToString())
cblRoles.Items[i].Selected = true;
}
Or you can use foreach as below:
Here i have created looping through items of checkbox list using foreach & item will be made selected id its value will match RoleId .
foreach (ListItem li in cblRoles.Items)
{
if (rd.RoleID.ToString() == li.Value)
{
li.Selected = true;
}
else
{
li.Selected = false;
}
}