Howard Bash
asked on
How to read SharePoint GridView with artificial Checkbox
I have a web part that has a gridview control on it. I get a data table from a library method I wrote and bind it to the grid. I can either have the grid make the columns with autogeneratcolumns set to true or I can iterate through the data table columns and build the grid columns when the page loads. All that works fine.
I need to add a fake checkbox column and detect which row is checked in a button click and retrieve a column of data from that checked row. I added a checkbox column to the grid in the designer and it is a template column with an edit and item template. I cannot seem to tell if it is checked.
I tried something like the following:
//Iterate through the Rows property
foreach (GridViewRow row in this.GridView1.Rows)
{
// Access the CheckBox
//tried that too ==> CheckBox cb = (CheckBox)row.FindControl( "CheckBox1 ");
CheckBox cb = row.Cells[0].Controls[1] as CheckBox;
if (cb != null)
{
if (cb.Checked)
{
acntList.Add("Some Account");
return acntList;
}
}
}
Any ideas on this would be greatly appreciated.
I need to add a fake checkbox column and detect which row is checked in a button click and retrieve a column of data from that checked row. I added a checkbox column to the grid in the designer and it is a template column with an edit and item template. I cannot seem to tell if it is checked.
I tried something like the following:
//Iterate through the Rows property
foreach (GridViewRow row in this.GridView1.Rows)
{
// Access the CheckBox
//tried that too ==> CheckBox cb = (CheckBox)row.FindControl(
CheckBox cb = row.Cells[0].Controls[1] as CheckBox;
if (cb != null)
{
if (cb.Checked)
{
acntList.Add("Some Account");
return acntList;
}
}
}
Any ideas on this would be greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here is the simple fragment with the test condition added:
if (!Page.IsPostBack)
{
this.GridView1.AutoGenerat
this.GridView1.AllowPaging
this.GridView1.PageSize = 20;
this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();
}
Now I can see the values and use them. Here is that piece which is used on a button click to get data for further processing within the web part:
/// <summary>
/// Routine used for Debug Only
/// </summary>
void ReadAllGridValues()
{
string val = string.Empty;
try
{
foreach (GridViewRow Item in this.GridView1.Rows)
{
CheckBox cb2 = (CheckBox)Item.FindControl
//cb.Checked
if (cb2.Checked)
{
String Source = Item.Cells[1].Text;
String FirstName = Item.Cells[2].Text;
String LastName = Item.Cells[3].Text;
String FriendlyName = Item.Cells[4].Text;
String Email = Item.Cells[5].Text;
String Account = Item.Cells[6].Text;
}
}
}
catch (Exception ex)
{
string emsg = ex.Message.ToString();
emsg += "||" + ex.StackTrace.ToString();
}
}
This works fine "unless" I do a page refresh and click the button again. At that point I get an "Index" out of range message. I know I am missing something subtle/trivial here. I would remove the check for post back but then I don't see the data to use.
Please advise. I am close but not quite there yet and a bit of wisdom to steer here would be great.