By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Getting value from cell in datagrid

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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[i][j].ToString() == "0")
ds.Tables[0].Rows[i][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.
If the value in column 3 reads 0, I want to change it to read YES.
How can I accomplish this task.
Thanks,
Dave


User submitted from AEWNET (http://www.aewnet.com/)
Nov 16 '05 #2

P: n/a
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.Alternating Item))
{
if(XXXX.Items[e.Item.ItemIndex].Cells[2].Text == "0")
XXXX.Items[e.Item.ItemIndex].Cells[2].Text = "YES";
}
}

HTH

Ollie Riches

"kscdavefl" <ks*******@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
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

Nov 16 '05 #3

P: n/a
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.Alternating Item))
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.g et_Item(Int32 index)
DataViewer.permissionForm.applicationPermissionGri d_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(Int3 2 itemIndex, Int32
dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem,
DataGridColumn[] columns, TableRowCollection rows, PagedDataSource
pagedDataSource)
System.Web.UI.WebControls.DataGrid.CreateControlHi erarchy(Boolean
useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBindi ng(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
DataViewer.permissionForm.FillApplicationPermissio nGrid() 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" wrote:
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.Alternating Item))
{
if(XXXX.Items[e.Item.ItemIndex].Cells[2].Text == "0")
XXXX.Items[e.Item.ItemIndex].Cells[2].Text = "YES";
}
}

HTH

Ollie Riches

"kscdavefl" <ks*******@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
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


Nov 16 '05 #4

P: n/a
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" <ks*******@discussions.microsoft.com> wrote in message
news:8E**********************************@microsof t.com...
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.Alternating Item))
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.g et_Item(Int32 index)
DataViewer.permissionForm.applicationPermissionGri d_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(Int3 2 itemIndex, Int32
dataSourceIndex, ListItemType itemType, Boolean dataBind, Object dataItem,
DataGridColumn[] columns, TableRowCollection rows, PagedDataSource
pagedDataSource)
System.Web.UI.WebControls.DataGrid.CreateControlHi erarchy(Boolean
useDataSource)
System.Web.UI.WebControls.BaseDataList.OnDataBindi ng(EventArgs e)
System.Web.UI.WebControls.BaseDataList.DataBind()
DataViewer.permissionForm.FillApplicationPermissio nGrid() 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" wrote:
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.Alternating Item))
{
if(XXXX.Items[e.Item.ItemIndex].Cells[2].Text == "0")
XXXX.Items[e.Item.ItemIndex].Cells[2].Text = "YES";
}
}

HTH

Ollie Riches

"kscdavefl" <ks*******@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com...
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


Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.