Changing a cell in a DataBount DataGrid
-
I have a
DataGrid
bound to a table. This is working fine. A column in the table can have null values. If the value is null then there is nothing printed in that cell in theDataGrid
I want that cell to contain "NONE" as its value I wrote the code likeif(DG1.Items.Count!=0)
{
int i=0;
foreach(DataGridItem item in DG1.Items)
{
if(ds.Tables["table1"].Rows[i]["someval"]==DBNull.Value)
{
item.Cells[5].Text="None";
}
i++;
}
}My DG1 is Bound to table1 previously. HELP _____________________________________________________ Yea! I could be wrong...
-
I have a
DataGrid
bound to a table. This is working fine. A column in the table can have null values. If the value is null then there is nothing printed in that cell in theDataGrid
I want that cell to contain "NONE" as its value I wrote the code likeif(DG1.Items.Count!=0)
{
int i=0;
foreach(DataGridItem item in DG1.Items)
{
if(ds.Tables["table1"].Rows[i]["someval"]==DBNull.Value)
{
item.Cells[5].Text="None";
}
i++;
}
}My DG1 is Bound to table1 previously. HELP _____________________________________________________ Yea! I could be wrong...
Change the values from your datatable instead, then rebind it to your datagrid. Basically the same for each loop, but replace the save value you checked from your dataset. the, once all the values are set, DG.datasource = ds.tables["table1"] DG.databind Jon G www.Gizmocoder.com
-
Change the values from your datatable instead, then rebind it to your datagrid. Basically the same for each loop, but replace the save value you checked from your dataset. the, once all the values are set, DG.datasource = ds.tables["table1"] DG.databind Jon G www.Gizmocoder.com
Jon G wrote: Change the values from your datatable instead, then rebind it to your datagrid. Basically the same for each loop, but replace the save value you checked from your dataset. How do I shange the values returened by a query like
Select * From table1
and shange a returned column ? Please elaborate.... I'm a novice..:sigh: _____________________________________________________ Yea! I could be wrong... -
Jon G wrote: Change the values from your datatable instead, then rebind it to your datagrid. Basically the same for each loop, but replace the save value you checked from your dataset. How do I shange the values returened by a query like
Select * From table1
and shange a returned column ? Please elaborate.... I'm a novice..:sigh: _____________________________________________________ Yea! I could be wrong...When performing a select query, as you have mentioned, you probably fill a datatable, or a dataset with the returned data. The dataset is a representation of the data found in the database, and is not linked in any way to the database. For example, lets say you filled a dataset with a "select * from table1" statement:
Dim myConnection As SqlConnection = New SqlConnection("Network Library=DBMSSOCN;Data Source=DBNAME,1433;Initial Catalog=CATALOGNAME;Trusted_Connection=yes;MAX POOL SIZE = 1000")
Dim myCommand As SqlCommand
Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter
Dim myQuery As StringmyDataSet = New DataSet("test") myDataSet.Tables.Add("testtable") myQuery = "select \* from sighting where surveyid = 1" myCommand = New SqlCommand(myQuery, myConnection) Try myConnection.Open() myDataAdapter.SelectCommand = myCommand myDataAdapter.Fill(myDataSet, "testtable")
Catch ex As Exception
Response.Write(ex.Message)
Response.Write(ex.Source)
Response.Write(ex.StackTrace)
Finally
myConnection.Close()
myConnection.Dispose()
myCommand.Dispose()
myDataAdapter.Dispose()
End TryOkay, now you have a dataset, with a table called "Testtable" that contains the results of your query. To alter a value contained inside that dataset, you can do something like this:
myDataSet.Tables("testtable").Rows(0).Item(2) = "SOMETHING"
Where Rows(#) is the row to edit Where Item(#) is the column to edit Once you have edited the dataset to your linkign, you bind it to your datagrid:
MyDATAGRID.Datasource = myDataSet.Tables("TestTable")
MyDATAGRID.DataBind()Jon G www.Gizmocoder.com
-
I have a
DataGrid
bound to a table. This is working fine. A column in the table can have null values. If the value is null then there is nothing printed in that cell in theDataGrid
I want that cell to contain "NONE" as its value I wrote the code likeif(DG1.Items.Count!=0)
{
int i=0;
foreach(DataGridItem item in DG1.Items)
{
if(ds.Tables["table1"].Rows[i]["someval"]==DBNull.Value)
{
item.Cells[5].Text="None";
}
i++;
}
}My DG1 is Bound to table1 previously. HELP _____________________________________________________ Yea! I could be wrong...
Handle the ItemDataBound event
private void DataGrid_ItemDataBound(object sender, DataGridItemEventArgs e) { if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem ) { DataRowView row = (DataRowView)e.Item.DataItem; if( row != null ) { if( row["mycol"] == null ) e.Item.Cells[5].Text = "None"; } }
-
Jon G wrote: Change the values from your datatable instead, then rebind it to your datagrid. Basically the same for each loop, but replace the save value you checked from your dataset. How do I shange the values returened by a query like
Select * From table1
and shange a returned column ? Please elaborate.... I'm a novice..:sigh: _____________________________________________________ Yea! I could be wrong...You sould change the query to explicity call the columns
SELECT Field1, Field2, ISNULL(Field3, 'None') FROM table1
If you are not using stored procs you should, rather then using query string. -
Handle the ItemDataBound event
private void DataGrid_ItemDataBound(object sender, DataGridItemEventArgs e) { if ( e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem ) { DataRowView row = (DataRowView)e.Item.DataItem; if( row != null ) { if( row["mycol"] == null ) e.Item.Cells[5].Text = "None"; } }
-
I have a
DataGrid
bound to a table. This is working fine. A column in the table can have null values. If the value is null then there is nothing printed in that cell in theDataGrid
I want that cell to contain "NONE" as its value I wrote the code likeif(DG1.Items.Count!=0)
{
int i=0;
foreach(DataGridItem item in DG1.Items)
{
if(ds.Tables["table1"].Rows[i]["someval"]==DBNull.Value)
{
item.Cells[5].Text="None";
}
i++;
}
}My DG1 is Bound to table1 previously. HELP _____________________________________________________ Yea! I could be wrong...