ASPxGridView.GetChildDataRow is always null - asp.net

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);
}
}
}

Related

How to store an array and make it available after postback

I need some help keeping an array after postback.
On page load I am creating a array that contains three random numbers and sorts them from lowest to highest. These numbers are then used to compare characters stored in another string in the positions of the generated numbers.
When the submit button is hit the numbers are regenerated and the verification fails as now there is a new set of numbers.
here is part of the code ... the array i need to keep is rannumInt.
int[] rannumInt = { 0, 0, 0 };
protected void page_load()
{
if (!IsPostBack)
{
int i, j;
if (rannumInt[0] == 0)
{
for (i = 0; i < 3; i++)
{
Random rannum = new Random();
rannumInt[i] = rannum.Next(1, 9);
if (i > 0)
{
for (j = 0; j < i; j++)
{
if (rannumInt[i] == rannumInt[j])
{
i--;
}
}
}
}
Array.Sort(rannumInt);
Label1.Text = Convert.ToString(rannumInt[0]);
TextBox1.Text = Convert.ToString(rannumInt[0]);
Label2.Text = Convert.ToString(rannumInt[1]);
TextBox2.Text = Convert.ToString(rannumInt[1]);
Label3.Text = Convert.ToString(rannumInt[2]);
TextBox3.Text = Convert.ToString(rannumInt[2]);
}
}
else
{
}
}
You will have to store rannumInt somewhere, like a Session or ViewState. Then when a PostBack occurs, convert the ViewState back to an array.
if (!Page.IsPostBack)
{
if (rannumInt[0] == 0)
{
Array.Sort(rannumInt);
Label1.Text = Convert.ToString(rannumInt[0]);
//save the array into the viewstate
ViewState["rannumInt"] = rannumInt;
}
}
else
{
//check if the viewstate exists
if (ViewState["rannumInt"] != null)
{
//convert it back to an int[]
rannumInt = ViewState["rannumInt"] as int[];
}
}
If you want the number to be the same across multiple pages and/or page reloads, use Session

Devexpress MultiSelect Row Field Name .?

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")); }
}
}

How to make dynamic column header clickable in gridview

I have a gridview with many columns. It's sortable, Allow Sorting="True", each column has Sort Expression. For each column sorting works just fine, except for 10 columns that have dynamic headers that I assign in Row_Databound event:
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
if (Session["Label" + i.ToString()] !=null)
{
e.Row.Cells[i].Text = Session["Label" + i.ToString()].ToString();
}
}
}
}
These 10 columns are not clickable. Is there any way to make them clickable? Everything else in these columns is enabled for sorting.
I've got some suggestions from a different forum about creating columns in Page_Load or Page_Init events, but this probably won't work for me.
Thank you.
You can replace the text of the existing LinkButton in the header cell:
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
string caption = Session["Label" + i.ToString()] as string;
if (caption != null)
{
TableCell headerCell = e.Row.Cells[i];
LinkButton lnkSort = headerCell.Controls[0] as LinkButton;
lnkSort.Text = caption;
}
}
}
}
It can be done. If you look at the HTML code you will see something similar to this as the link for sorting the GridView.
yourColumnName
We need to recreate that link in the RowDataBound function.
for (int i = 1; i < 11; i++)
{
//first we cast the sender as a gridview
GridView gv = sender as GridView;
//get the unique ID of the gridview, this is different from ClientID which you normally would use for JavaScipt etc
string uniqueID = gv.UniqueID;
//then get the SortExpression for the column
string sortExpression = gv.Columns[i].SortExpression;
//get the new column name from the session
string yourColumnName = string.Empty;
if (Session["Label" + i.ToString()] != null)
{
yourColumnName = Session["Label" + i.ToString()].ToString();
}
//and then we fill the header with the new link
e.Row.Cells[i].Text = "" + yourColumnName + "";
}
However for this to work, enableEventValidation has to be set to false, which is NOT recommended. Otherwise you will get the "Invalid postback or callback argument" error.
Better would be changing the column names somehow before the data is bound to the gridview.
Thank you very much for your help. The solution with LinkButton worked great for me:
protected void gvSearchResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 1; i < 11; i++)
{
if (Session["Label" + i.ToString()] !=null)
{
((LinkButton)(e.Row.Cells[i].Controls[0])).Text = Session["Label" + i.ToString()].ToString();
}
}
}
}

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.

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];

Resources