Devexpress MultiSelect Row Field Name .? - devexpress

How can I determine whether Xtragrid in multiselect=True mode check button is selected during the GridviewButton event? See the grid image below:

Solved --
gridView1_SelectionChanged
var view = sender as GridView;
if (view == null) return;
view.BeginSelection();
int[] selectedRows = view.GetSelectedRows();
int[] RowHandle = view.GetSelectedRows();
try
{
for (int i = -1; i < selectedRows.Length; i++)
{
if (selectedRows.Length == 0)
{
clear();
}
else
{
//Clear all value zero
foreach (int value in RowHandle)
{ deger += Convert.ToDouble(gridView1.GetRowCellValue(value, "FieldName")); }
}
}

Related

How to Create Dynamically Generated TextBoxes in ASP.NET

I have this code which generates TextBoxes in a Table dynamically, I got it from a website:
protected void AddStdBtn_Click(object sender, EventArgs e)
{
CreateDynamicTable();
}
private void CreateDynamicTable()
{
// Fetch the number of Rows and Columns for the table
// using the properties
if (ViewState["Stu"] != null)
Students = (DataTable)ViewState["Stu"];
int tblCols = 1;
int tblRows = Int32.Parse(NoTxt.Text);
// Now iterate through the table and add your controls
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
txtBox.ID = "txt-" + i.ToString() + "-" + j.ToString();
txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
}
// Add the TableRow to the Table
tbl.Rows.Add(tr);
tbl.EnableViewState = true;
ViewState["tbl"] = true;
}
}
protected void CalculateBtn_Click(object sender, EventArgs e)
{
foreach (TableRow tr in tbl.Controls)
{
foreach (TableCell tc in tr.Controls)
{
if (tc.Controls[0] is TextBox)
{
Response.Write(((TextBox)tc.Controls[0]).Text);
}
}
Response.Write("<br/>");
}
}
protected override object SaveViewState()
{
object[] newViewState = new object[2];
List<string> txtValues = new List<string>();
foreach (TableRow row in tbl.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[0] is TextBox)
{
txtValues.Add(((TextBox)cell.Controls[0]).Text);
}
}
}
newViewState[0] = txtValues.ToArray();
newViewState[1] = base.SaveViewState();
return newViewState;
}
protected override void LoadViewState(object savedState)
{
//if we can identify the custom view state as defined in the override for SaveViewState
if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
{
object[] newViewState = (object[])savedState;
string[] txtValues = (string[])(newViewState[0]);
if (ViewState["Stu"] != null)
Students = (DataTable)ViewState["Stu"];
if (txtValues.Length > 0)
{
//re-load tables
CreateDynamicTable();
int i = 0;
foreach (TableRow row in tbl.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[0] is TextBox && i < txtValues.Length)
{
((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();
}
}
}
}
//load the ViewState normally
base.LoadViewState(newViewState[1]);
}
else
{
base.LoadViewState(savedState);
}
}
Now if you examine CreateDynamicTable() function, particularly the line:
int tblRows = Int32.Parse(NoTxt.Text);
you can see that the user can control the number of rows by entering a number in the TextBox NoTxt. When I run this code I get the following error:
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: Input string was not in a correct format.
and the error location is in the same line above.
The question is: How can I let the user control the number of rows in the table?
After few days of reading and research, the solution can be done by using Session variables. Store the value obtained in NoTxt.Text as follows for example:
Session["V"] = Int32.Parse(NoTxt.Text);
and retrieve it once you need it.

ASPxGridView.GetChildDataRow is always null

I'm dynamically binding my grid with a result of linq expression on PageLoad and at HtmlRowPrepared event i'm trying to reach a DataRow with
for (int i = 0; i < grid.GetChildRowCount(visibleGrIndex); i++)
{
var row = grid.GetChildDataRow(visibleGrIndex, i);
}
but its ALWAYS NULL?
HtmlRowPrepared is triggered once for every grid row.
So, you can use this code to fetch data row:
private void Grid_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e) {
if (e.RowType == GridViewRowType.Group)
{
for (int i = 0; i < GetChildRowCount(e.VisibleIndex); i++)
{
var row = GetChildDataRow(e.VisibleIndex, i);
}
}
}

Deleting records from table

In the above photo I have a table containing a list of users that are add dynamically in the table.
I want when I click on the red-cross image button the corresponding user to be deleted from the database.
Here is the code that fills the table:
/****** Filling the originators table ********/
string[] oa1 = originator;
for (int i = 0; i < oa1.Length; i++)
{
TableRow row = new TableRow();
users_table.Rows.Add(row);
for (int colCount = 0; colCount < 4; colCount++)
{
TableCell cell = new TableCell();
row.Cells.Add(cell);
if (colCount == 0)
{
cell.Controls.Add(new LiteralControl(oa1[i]));
}
else if (colCount == 1)
{
cell.Controls.Add(new LiteralControl("|"));
}
else if (colCount == 2)
{
LLKB userInfo = new LLKB();
cell.Controls.Add(new LiteralControl(userInfo.InfoGetter(oa1[i].Trim(), "name")));
}
else if (colCount == 3)
{
ImageButton btn = new ImageButton();
btn.ImageUrl = "img/DeleteRed.png";
btn.ID = oa1[i] + "_" + i;
btn.Click += new ImageClickEventHandler(delete_originator);
cell.Controls.Add(btn);
}
}
}
and here is the method that show do the deletion:
public void delete_originator(object sender, ImageClickEventArgs e)
{
//some code here
}
So, what do you suggest I write in the deletion method??
or if you have another idea...
you should empty table and recreate the table and get username thorugh spliting imagebutton ID
by '_',So in loop when that username come you add continue statement.
some thing like that in Delete event :
string Username=((imagebutton)sender).ID.toString().tosplit('_')[0];

how to check items in checkboxlist?

i have a checkboxlist that i want to check some of it's items,
items that i wanna check store in database, i'm selecting them from database and in a loop i wrote thees but just last item selected :(( :
var selectedRoles = (from r in DataContext.Context.Core_PagesPermission
where r.PageName.Contains(pageName) select r)
.FirstOrDefault();
if(selectedRoles != null)
{
string roles = selectedRoles.Roles;
string[] role = roles.Split(',');
int countTags = role.Count();
foreach (string word in role)
{
CheckBoxList1.SelectedValue = word;
Response.Write(word + "<br/>");
countTags -= 1;
}
}
this worked :
var selectedRoles = (from r in DataContext.Context.Core_PagesPermission where r.PageName.Contains(pageName) select r)
.FirstOrDefault();
dsRoles.DataBind();
CheckBoxList1.DataBind();
if(selectedRoles != null)
{
string roles = selectedRoles.Roles;
string[] role = roles.Split(',');
foreach (string word in role)
{
try
{
CheckBoxList1.Items.FindByValue(word).Selected = true;
}
catch (Exception exp)
{
lbError.Text= exp.ToString();
}
You will want to select the individual items:
CheckBoxList1.Items.FindByText(word).Selected = true;
or
CheckBoxList1.Items.FindByValue(word).Selected = true;
However, if the checkboxlist doesn't have the text/value you are looking for, it will throw an error.
datatable method checkboxlist
for (int i = 0; i < dt.Rows.Count; i++)
{
chkCategories.Items.FindByValue(dt.Rows[i]["CategoryID"].ToString()).Selected = true;
}

Dynamically add checkboxlist into placeholder and get the checked value of the checkboxlist

I am creating an admin Page, in which checkboxlist(User list from DB)is to be created dynamically and its the checked users value is retrieved.
There are different types of users, so it is distinguised groupwise.
Now first a panel is defined, the checkboxlist is created dynamically and placed inside the panel, then the panel is placed inside a Placeholder.
What Iam doing here is placing the checkboxlist inside the panel, then the panel inside the placeholder. So the values of the Checkboxlist is not retrieved, due to the panel it doesnt get the checkboxlist and it doesn't loop through the Checkboxlist.
What I have done is.
private void AddControl(string pUserGrp, int pUserGrp_Id, int pName)
{
CheckBoxList chkList = new CheckBoxList();
CheckBox chk = new CheckBox();
User us = new User();
us.OrderBy = "Order By User_Name";
us.WhereClause = "Where UserRole_Id = " + pUserGrp_Id ;
chkList.ID = "ChkUser" + pName ;
chkList.AutoPostBack = true;
chkList.Attributes.Add("onClick", "getVal(ChkUser" + pName + ");");
chkList.RepeatColumns = 6;
chkList.DataSource = us.GetUserDS();
chkList.DataTextField = "User_Name";
chkList.DataValueField = "User_Id";
chkList.DataBind();
chkList.Attributes.Add("onClick", "getVal(this);");
Panel pUser = new Panel();
if (pUserGrp != "")
{
pUser.GroupingText = pUserGrp ;
chk.Text = pUserGrp;
}
else
{
pUser.GroupingText = "Non Assigned Group";
chk.Text = "Non Assigned group";
}
pUser.Controls.Add(chk);
pUser.Controls.Add(chkList);
Place.Controls.Add(pUser);
}
private void setChecked(int pPageGroupId)
{
ArrayList arr = new ArrayList();
PageMaster obj = new PageMaster();
obj.WhereClause = " Where PageGroup_Id = " + pPageGroupId;
arr = obj.GetPageGroupUserRights(null);
CheckBoxList chkList = (CheckBoxList)Place.FindControl("ChkUser");
if (chkList != null)
{
for (int i = 0; i < chkList.Items.Count; i++)
{
if (arr.Count > 0)
{
int ii = 0;
while (ii < arr.Count)
{
PageMaster oCand = (PageMaster)arr[ii];
if (oCand.User_Name == chkList.Items[i].Text)
{
if (!chkList.Items[i].Selected)
{
chkList.Items[i].Selected = true;
}
}
ii++;
oCand = null;
}
}
}
}
}
public string GetListCheckBoxText()
{
StringBuilder sbtext = new StringBuilder();
foreach (Control c in Place.Controls)
{
if (c.GetType().Name == "CheckBoxList")
{
CheckBoxList cbx1 = (CheckBoxList)c;
foreach (ListItem li in cbx1.Items)
{
if (li.Selected == true)
{
sbtext.Append(",");
sbtext.Append(li.Value);
}
else
{
sbtext.Append(li.Value);
}
}
}
}
return sbtext.ToString(); }
It doesnt get through the Checkboxlist control in the setChecked(), also doesnt loop through the GetListCheckBoxTest().
Anyone can plz help me.
Regards
The problem is that you are trying to find a control (in setChecked) without setting the Name property. You are using this:
CheckBoxList chkList = (CheckBoxList)Place.FindControl("ChkUser");
But where is this in AddControl?
chkList.Name = "ChkUser";
And in GetListCheckBoxText instead of...
if (c.GetType().Name == "CheckBoxList")
...use this:
if (c.GetType()== typeof(CheckBoxList))

Resources