Answered by:
Gridview problem!

Question
-
User105119270 posted
Here is my code
using System; using System.Data; using System.Web; using System.Web.UI.WebControls; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; namespace LessonLookUp { public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart { SPGridView oGrid; DataTable tbl = null; protected override void Render(System.Web.UI.HtmlTextWriter writer) { SPWeb sites = Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(Context); SPSiteDataQuery qry = new SPSiteDataQuery(); qry.Query = ""; //qry.Query = "<OrderBy><FieldRef Name='Author' /></OrderBy>"; qry.Lists = "<Lists ServerTemplate='850' BaseType='1' />"; qry.ViewFields = @"<FieldRef Name='Title'/>"; qry.Webs = "<Webs Scope='SiteCollection' />"; tbl = new DataTable(); tbl = sites.GetSiteData(qry); oGrid.DataSource = tbl; oGrid.DataBind(); oGrid.PagerTemplate = null; base.Render(writer); } protected override void CreateChildControls() { oGrid = new SPGridView(); oGrid.ID = "gridList"; oGrid.AutoGenerateColumns = false;
SPBoundField title = new SPBoundField(); title.DataField = "Title"; title.HeaderText = "Lesson Title"; this.oGrid.Columns.Add(title); ButtonField view = new ButtonField(); view.ButtonType = ButtonType.Button; view.HeaderText = "Option 1"; view.Text = "Delete"; oGrid.RowCommand += new GridViewCommandEventHandler(gl_RowCommand); this.oGrid.Columns.Add(view); this.Controls.Add(this.oGrid); this.oGrid.PagerTemplate = null; base.CreateChildControls(); } void gl_RowCommand(Object sender, GridViewCommandEventArgs e) { int idx = Convert.ToInt32(e.CommandArgument); GridViewRow row = oGrid.Rows[idx]; Label lbl1 = (Label)row.Cells[0].Controls[0]; string strValue = lbl1.Text; HttpContext.Current.Response.Write(strValue);
}
} }
It shows the data, however when I press a 'ButtonField view' I get error
"Index was out of range. Must be non-negative and less than the size of the collection"
Any ideas?
Wednesday, April 30, 2008 4:25 AM
Answers
-
User-1125720926 posted
one of the indexes (passed in square brackets [ ] ) is out of range..
ogrid.Rows[idx] - does idx reference a row index? idx is worked out from the commandArgument - is that returning the right value?
row.Cells[0] - is there a cell at index 0?
row.Cells[0].Controls[0] - is there a control in the cell?
hope that helps
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 30, 2008 6:26 AM
All replies
-
User-1125720926 posted
you'll get this error if you try to access a property or method on an object which is 'null'
any of these lines could be the culprit:
GridViewRow row = oGrid.SelectedRow;
Label lbl1 = (Label)row.Cells[0].Controls[0];
string strValue = lbl1.Text;
is 'oGrid' null? is 'row' null? is row.Cells[0] returning null? etc...
somewhere here looks like most likely : (Label)row.Cells[0].Controls[0]
Wednesday, April 30, 2008 5:41 AM -
User105119270 posted
(what use is asking for help when I cant get the question right)
sorry it should of been:
void gl_RowCommand(Object sender, GridViewCommandEventArgs e) { int idx = Convert.ToInt32(e.CommandArgument); GridViewRow row = oGrid.Rows[idx]; Label lbl1 = (Label)row.Cells[0].Controls[0]; string strValue = lbl1.Text; HttpContext.Current.Response.Write(strValue); }
which returns
"Index was out of range.Must be non-negative and less than the size of the collection"
:(
Wednesday, April 30, 2008 6:22 AM -
User-1125720926 posted
one of the indexes (passed in square brackets [ ] ) is out of range..
ogrid.Rows[idx] - does idx reference a row index? idx is worked out from the commandArgument - is that returning the right value?
row.Cells[0] - is there a cell at index 0?
row.Cells[0].Controls[0] - is there a control in the cell?
hope that helps
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, April 30, 2008 6:26 AM -
User105119270 posted
The code works fine, I get two columns(Title, Button) with lots of rows. But when I click a button I get "Index was out of range...
int idx = Convert.ToInt32(e.CommandArgument);
Above returns the correct number! but when I doGridViewRow row = oGrid.Rows[idx];
Wednesday, April 30, 2008 6:33 AM