i have a Following Arraylist that carries the List of Arraylist Values of String type. I want to Disable a Cell that does not match with the Values in the Arraylist.
Code:
ArrayList arr = Disable_Grid();
foreach(arr in <b>ultraGridCycles</b>)
{
<b>//hide the Cell </b>;
}
Thank you
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
Here is how you do it. I am giving a sample application code which creates a data table and binds it to a gridview. The data table contains 3 columns and 5 rows with the first column having the value "0" and the second column the value "1" and the third column the value "2" in all the 5 rows. It also creates an ArrayList which contains 2 elements with strings "2" and "3". The data table is bound to the grid view. All the cells of the gridview that contain values equal to any of the strings in the arraylist get hidden. So all the cells in third column of the grid view containing the string "2" get hidden. This is a sample application. I am giving the code below for a Default page. You can open up a default application in VS 2005 and copy the code I give below into the html page and the code behind and try it out.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* Create the data table which you are going to bind to the Gridview.
* After executing this code the data table contains 3 rows and 3 columns
* And in all three rows the first column contains "0" the scond contains
* "1" and the third contains "2" */
DataTable dt = new DataTable();
dt.Columns.Add("Column0", System.Type.GetType("System.String"));
dt.Columns.Add("Column1", System.Type.GetType("System.String"));
dt.Columns.Add("Column2", System.Type.GetType("System.String"));
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
for (int j = 0; j < 3; j++)
{
int p = j;
dt.Rows[i][j] = p.ToString();
}
}
mygridview.DataSource = dt.DefaultView;
mygridview.DataBind();
int v = dt.Rows.Count;
ArrayList array = new ArrayList(2);
for (int i = 2, j = 0; j < 2; i++, j++)
array.Insert(j, i.ToString());
int u = mygridview.Rows.Count;
for (int i = 0; i < mygridview.Rows.Count; i++)
{
GridViewRow g = mygridview.Rows[i];
for(int j = 0; j < g.Cells.Count; j++)
{
for (int p = 0; p < array.Count; p++)
{
string txt = g.Cells[j].Text;
if (txt == array[p].ToString())
mygridview.Rows[i].Cells[j].Visible = false;
}
}
}
}
}
Hope you can adapt this code to suit your purpose.
Cheers.
Originally Posted by vuyiswam
Good day All
i have a Following Arraylist that carries the List of Arraylist Values of String type. I want to Disable a Cell that does not match with the Values in the Arraylist.
Code:
ArrayList arr = Disable_Grid();
foreach(arr in <b>ultraGridCycles</b>)
{
<b>//hide the Cell </b>;
}
Thank you very much for your reply. I get Confused by the Loops.
Can you please Explain or Comment the Code you Send me line by line. I dont want to get the job done only but i need to understand it.
I thank you for your Help
Vuyiswa Maseko
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
I have simplified the code as much as I can and put in proper comment entries. The code is also slightly changed so that the principle by which it works is demonstrated properly. I hope this helps you in understanding what the code is and how it gets executed.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* The following line of code creates a blank data table
* object with no rows or columns. */
DataTable dt = new DataTable();
/* The following 3 statements add 3 columns with names "Column0"
* "Column1" and "Column2" to the data table created above.
* All the columns are created are of string data type */
dt.Columns.Add("Column0", System.Type.GetType("System.String"));
dt.Columns.Add("Column1", System.Type.GetType("System.String"));
dt.Columns.Add("Column2", System.Type.GetType("System.String"));
/* The following "for" loop gets executed 5 times and adds five empty
* rows to the data table which we have already created */
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow(); // Create a datarow
dt.Rows.Add(dr); // Add it to the data table created above.
}
/* Now we can add data to each column of the 5 rows
* that have been created and added to the data table.
* For that I will use another "for" loop which I give
* below. This loop again gets executed 5 times. Once each
* for putting values into the columns of each row. This
* loop works in such a way that the 0th row will have values
* "0", "1", "2". The 1st row will have values "3", "4", "5"
* and so on and so forth till the last row */
int x = 0;
for (int i = 0; i < 5; i++)
{
dt.Rows[i]["Column0"] = x.ToString();
x++;
dt.Rows[i]["Column1"] = x.ToString();
x++;
dt.Rows[i]["Column2"] = x.ToString();
x++;
}
/* The following 2 statements set the data source of the gridview to the
* DefaultView of the created table and bind it so that the table created
* appears in a grid when the page is run. In this grid the 1st row will have
* "0", "1" and "2" and the 2nd will have "3", "4" and "5" and so on and so
* forth. */
mygridview.DataSource = dt.DefaultView;
mygridview.DataBind();
/*The follwoing statement creates an array list with 2 elements */
ArrayList array = new ArrayList(2);
// Set j = 2
int j = 1;
/* Now the following for loop executes twice. The first
* time it executes it inserts the string "1" into the
* 0th position or the first element of the arraylist.
* The second time it executes it inserts the string "2"
* into the 1st position. So array[0] contains "1" and
* array[1] contains "2" */
for (int i = 0; i < 2; i++)
{
array.Insert(i, j.ToString()); //Insert value into the i'th position of arraylist and set it to j.
j++; // increment j
}
/* Set the variable u to the total number of rows in the grid.
* Since the total number of rows in the data table was 5 the
* total number of rows in the grid will also be 5. So u will
* become equal to 5 at runtime */
int u = mygridview.Rows.Count;
/* There are three nested "for" looops in the following code.
* What this code does is to examine each cell of the grid view and
* if it matches any of the values in the arrayliat then that cell
* is made invisible. As we know our arraylist contains "1" and "2".
* So in the grid view the cells where "1" and "2" are meant to be
* will be blank now. These are the 2nd and 3rd columns of the
* 1st row of the grid view. So when you run the page those 2 cells
* will be blank.*/
for (int i = 0; i < u; i++) // The outer for loop executes u times. At runtime u would be 5. So it executes 5 times.
{
/* Pull out the i'th grid view row. So if i is 0, g will contain the 0th row
* of the grid, and if it is 1 it will pull out the 1st row of the grid and so on.*/
GridViewRow g = mygridview.Rows[i];
/* There are 3 columns in the grid because the table it was bound to
* had 3 columns. So each row will contain 3 cells. So g.Cells.Count
* would be 3 at runtime. So the following loop gets executed 3 times */
for (int t = 0; t < g.Cells.Count; t++)
{
string txt = g.Cells[t].Text; // Pull out the contents of the t'th cell and store it in the variable txt
/* Store the total number of elements in the arraylist in the variable m.
* Since there are 2 elements in our arraylist m will have the value 2
* at runtime */
int m = array.Count;
/* The following "for" loop gets executed m times, that is 2 times at runtime
* On each pass it compares the contents of the variable txt which actually is
* the grid cell content with the value in the array elelment and makes the cell
* invisible if they match */
for (int p = 0; p < m; p++)
{
string arrval = array[p].ToString(); // Store value in array[p] in string variable arrval
if (txt == arrval) // If arrval matches make the grid cell invisible.
g.Cells[t].Visible = false;
}
}
}
}
}
You can open a new web site and substitute the code for Default.aspx and Default.aspx.cs with the code above and run the application. Its working is explained by the comment entries.
Hope this helps.
Warm Regards.
Originally Posted by vuyiswam
Thank you very much for your reply. I get Confused by the Loops.
Can you please Explain or Comment the Code you Send me line by line. I dont want to get the job done only but i need to understand it.
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
Thank man for your Help. I still have a Problem. Please Check the Code Below
Code:
/*After the Grid has been Populated, let us remove some of the cells
* using the Following code
*/
ArrayList array = (ArrayList)Disable_Grid();
//Count the Number of rows in a gridview and initialize the integer with those values
int u = cycleGrid.Rows.Count;
/*Another For Loop,First we declare an Interger variable "i" and
* initialize it with "0" and as long as the variable i is less than the number of
* rows in a gridview increament the variable i
*/
for (int i = 0; i < cycleGrid.Rows.Count; i++)
{
/*Creating an object of a Class GridViewRow "g" and
* passing the Value a Variable i that has
* been checked as less than the Value of the Rows
*/
UltraGridRow g = cycleGrid.Rows[i];
/*Another For Loop. Initialization of a newly declared variable
* j to "0" and as long as the number of Cells are less than the variable J then increase the Value of a
* variable J
*/
for (int j = 0; j < g.Cells.Count; j++)
{
/*for Loop in a For loop. Declare an Integer "p" and Assign
* 0 to it and as long as the number of p variable are
* less than the number of indexes in the array list variable
if its true the value of P is incremented by 1*/
for (int p = 0; p < array.Count; p++)
{
/*A String is Declared and is Assigned a value of
* of a cell indexed by the variable J that is not supposed greater than
* the number of cells in a grid
*/
string txt = g.Cells[j].Text;
/*if then value in the text is equal to the value of the array
* make the cell invisible*/
int m = array.Count;
if (txt != array[p].ToString())
{
/*The I is representing the Rows and the J is representing the Cells
*/
cycleGrid.Rows[i].Cells[j].Style.ForeColor = System.Drawing.Color.Red;
}
else
{
cycleGrid.Rows[i].Cells[j].Text = "";
}
}
}
}
}
I just Extracted it from my code. Now it works but there is a logical Error here. The Bolded Part at the End is not Executed ,
What am i doing Wrong ?
In the Loop the Arraylist Varible carries the value of "4"
Code:
for (int p = 0; p < array.Count; p++)
{
but when it loops it does not bring anything a Value of 4 , and this will result in an empty grid. What the Code does will hide the "4" row and show the 3 Columns i have , Please Check the Example Image
Thank you
Last edited by vuyiswam; February 27th, 2009 at 07:45 AM.
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
If I understand you correctly if the cell does not contain a value that is equal to txt then you want to make it red in color and if it does contain a value equal to txt then you want to make it invisible. Then I suggest you replace your code with what I give below. I have highlighted the areas that have to be changed.
Code:
/*After the Grid has been Populated, let us remove some of the cells
* using the Following code
*/
ArrayList array = (ArrayList)Disable_Grid();
//Count the Number of rows in a gridview and initialize the integer with those values
int u = cycleGrid.Rows.Count;
/*Another For Loop,First we declare an Interger variable "i" and
* initialize it with "0" and as long as the variable i is less than the number of
* rows in a gridview increament the variable i
*/
for (int i = 0; i < cycleGrid.Rows.Count; i++)
{
/*Creating an object of a Class GridViewRow "g" and
* passing the Value a Variable i that has
* been checked as less than the Value of the Rows
*/
UltraGridRow g = cycleGrid.Rows[i];
/*Another For Loop. Initialization of a newly declared variable
* j to "0" and as long as the number of Cells are less than the variable J then increase the Value of a
* variable J
*/
for (int j = 0; j < g.Cells.Count; j++)
{
/*for Loop in a For loop. Declare an Integer "p" and Assign
* 0 to it and as long as the number of p variable are
* less than the number of indexes in the array list variable
if its true the value of P is incremented by 1*/
for (int p = 0; p < array.Count; p++)
{
/*A String is Declared and is Assigned a value of
* of a cell indexed by the variable J that is not supposed greater than
* the number of cells in a grid
*/
string txt = g.Cells[j].Text;
/*if then value in the text is equal to the value of the array
* make the cell invisible*/
int m = array.Count;
if (txt != array[p].ToString())
{
/*The I is representing the Rows and the J is representing the Cells
*/
cycleGrid.Rows[i].Cells[j].Style.Add("color", "red");
}
else
{
cycleGrid.Rows[i].Cells[j].Style.Add("display", "none");
}
}
}
}
}
In the code you have written you are using
Code:
cycleGrid.Rows[i].Cells[j].Text = "";
By doing this you are replacing the contents of the cell itself instead of just making it invisible. By changing it to what I have given you will make the contents of the cell invisible but it will still exist and so when you loop through it, it will bring a value of 4 even though it is invisible.
Hope this helps.
Warm Regards.
Originally Posted by vuyiswam
Thank man for your Help. I still have a Problem. Please Check the Code Below
Code:
/*After the Grid has been Populated, let us remove some of the cells
* using the Following code
*/
ArrayList array = (ArrayList)Disable_Grid();
//Count the Number of rows in a gridview and initialize the integer with those values
int u = cycleGrid.Rows.Count;
/*Another For Loop,First we declare an Interger variable "i" and
* initialize it with "0" and as long as the variable i is less than the number of
* rows in a gridview increament the variable i
*/
for (int i = 0; i < cycleGrid.Rows.Count; i++)
{
/*Creating an object of a Class GridViewRow "g" and
* passing the Value a Variable i that has
* been checked as less than the Value of the Rows
*/
UltraGridRow g = cycleGrid.Rows[i];
/*Another For Loop. Initialization of a newly declared variable
* j to "0" and as long as the number of Cells are less than the variable J then increase the Value of a
* variable J
*/
for (int j = 0; j < g.Cells.Count; j++)
{
/*for Loop in a For loop. Declare an Integer "p" and Assign
* 0 to it and as long as the number of p variable are
* less than the number of indexes in the array list variable
if its true the value of P is incremented by 1*/
for (int p = 0; p < array.Count; p++)
{
/*A String is Declared and is Assigned a value of
* of a cell indexed by the variable J that is not supposed greater than
* the number of cells in a grid
*/
string txt = g.Cells[j].Text;
/*if then value in the text is equal to the value of the array
* make the cell invisible*/
int m = array.Count;
if (txt != array[p].ToString())
{
/*The I is representing the Rows and the J is representing the Cells
*/
cycleGrid.Rows[i].Cells[j].Style.ForeColor = System.Drawing.Color.Red;
}
else
{
cycleGrid.Rows[i].Cells[j].Text = "";
}
}
}
}
}
I just Extracted it from my code. Now it works but there is a logical Error here. The Bolded Part at the End is not Executed ,
What am i doing Wrong ?
In the Loop the Arraylist Varible carries the value of "4"
Code:
for (int p = 0; p < array.Count; p++)
{
but when it loops it does not bring anything a Value of 4 , and this will result in an empty grid. What the Code does will hide the "4" row and show the 3 Columns i have , Please Check the Example Image
I would like to take this chance and thank you guys for helping me resolve the Current Problem. I was using Infragestic Controls and had a hard time using them. But with people like you online I made it. You are a star and you deserve ratings.
Thank you,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.