Getting value from cell in datagrid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I ahve a datagrid on a web form. I need to change the value in column 3 as
follows.

If the value in column 3 reads 0, I want to change it to read YES.

How can I accomplish this task.

Thanks,

Dave
 
If you filled the datagrid from a dataset, just iterate through your dataset, change the values:

for (int i=0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j=0; j < ds.Tables[0].Columns.Count ; j++)
{
if(ds.Tables[0].Rows[j].ToString() == "0")
ds.Tables[0].Rows[j] = "YES";
}
}
The datagrid to will to be updated with the new data.

k-lo
I ahve a datagrid on a web form. I need to change the value in column 3 as
follows.

User submitted from AEWNET (http://www.aewnet.com/)
 
subscribe to the DataBound event on the datagrid and subsititute the value
as required

e.g.

private void XXXX_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if((e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item) ||
(e.Item.ItemType ==
System.Web.UI.WebControls.ListItemType.AlternatingItem))
{
if(XXXX.Items[e.Item.ItemIndex].Cells[2].Text == "0")
XXXX.Items[e.Item.ItemIndex].Cells[2].Text = "YES";
}
}

HTH

Ollie Riches
 
I now get the following error:

Server Error in '/DataViewer' Application
--------------------------------------------------------------------------------

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of
range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 199: (e.Item.ItemType ==
System.Web.UI.WebControls.ListItemType.AlternatingItem))
Line 200: {
Line
201: if(applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[6].Text ==
"0")
Line
202: applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[0].Text =
"NO";
Line 203: }


Source File: c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs Line:
201

Stack Trace:


[ArgumentOutOfRangeException: Index was out of range. Must be non-negative
and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +91
System.Web.UI.WebControls.DataGridItemCollection.get_Item(Int32 index)
DataViewer.permissionForm.applicationPermissionGrid_ItemDataBound(Object
sender, DataGridItemEventArgs e) in
c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:201
System.Web.UI.WebControls.DataGrid.OnItemDataBound(DataGridItemEventArgs e)
System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32
dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem,
DataGridColumn[] columns, TableRowCollection rows, PagedDataSource
pagedDataSource)
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean
useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
DataViewer.permissionForm.FillApplicationPermissionGrid() in
c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:169
DataViewer.permissionForm.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:50
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET
Version:1.1.4322.573

What am I doing worng here? The code is as you sent to me.



Ollie Riches said:
subscribe to the DataBound event on the datagrid and subsititute the value
as required

e.g.

private void XXXX_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if((e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item) ||
(e.Item.ItemType ==
System.Web.UI.WebControls.ListItemType.AlternatingItem))
{
if(XXXX.Items[e.Item.ItemIndex].Cells[2].Text == "0")
XXXX.Items[e.Item.ItemIndex].Cells[2].Text = "YES";
}
}

HTH

Ollie Riches

kscdavefl said:
I ahve a datagrid on a web form. I need to change the value in column 3 as
follows.

If the value in column 3 reads 0, I want to change it to read YES.

How can I accomplish this task.

Thanks,

Dave
 
don't forget that collections in .Net start at index zero, so if you want
item six it would XXXXX[5]

HTH

Ollie Riches

kscdavefl said:
I now get the following error:

Server Error in '/DataViewer' Application.
-------------------------------------------------------------------------- ------

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of
range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 199: (e.Item.ItemType ==
System.Web.UI.WebControls.ListItemType.AlternatingItem))
Line 200: {
Line
201: if(applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[6].Text ==
"0")
Line
202: applicationPermissionGrid.Items[e.Item.ItemIndex].Cells[0].Text =
"NO";
Line 203: }


Source File: c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs Line:
201

Stack Trace:


[ArgumentOutOfRangeException: Index was out of range. Must be non-negative
and less than the size of the collection.
Parameter name: index]
System.Collections.ArrayList.get_Item(Int32 index) +91
System.Web.UI.WebControls.DataGridItemCollection.get_Item(Int32 index)
DataViewer.permissionForm.applicationPermissionGrid_ItemDataBound(Object
sender, DataGridItemEventArgs e) in
c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:201
System.Web.UI.WebControls.DataGrid.OnItemDataBound(DataGridItemEventArgs e)
System.Web.UI.WebControls.DataGrid.CreateItem(Int32 itemIndex, Int32
dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem,
DataGridColumn[] columns, TableRowCollection rows, PagedDataSource
pagedDataSource)
System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean
useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
DataViewer.permissionForm.FillApplicationPermissionGrid() in
c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:169
DataViewer.permissionForm.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\dataviewer\permissionform.aspx.cs:50
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()




-------------------------------------------------------------------------- ------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET
Version:1.1.4322.573

What am I doing worng here? The code is as you sent to me.



Ollie Riches said:
subscribe to the DataBound event on the datagrid and subsititute the value
as required

e.g.

private void XXXX_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if((e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item) ||
(e.Item.ItemType ==
System.Web.UI.WebControls.ListItemType.AlternatingItem))
{
if(XXXX.Items[e.Item.ItemIndex].Cells[2].Text == "0")
XXXX.Items[e.Item.ItemIndex].Cells[2].Text = "YES";
}
}

HTH

Ollie Riches

kscdavefl said:
I ahve a datagrid on a web form. I need to change the value in column
3
as
follows.

If the value in column 3 reads 0, I want to change it to read YES.

How can I accomplish this task.

Thanks,

Dave
 
Back
Top