Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Changing a cell in a DataBount DataGrid

Changing a cell in a DataBount DataGrid

Scheduled Pinned Locked Moved ASP.NET
help
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saud_a_k
    wrote on last edited by
    #1

    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 the DataGrid I want that cell to contain "NONE" as its value I wrote the code like

    if(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...

    J N S 3 Replies Last reply
    0
    • S saud_a_k

      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 the DataGrid I want that cell to contain "NONE" as its value I wrote the code like

      if(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...

      J Offline
      J Offline
      Jon G
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • J Jon G

        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

        S Offline
        S Offline
        saud_a_k
        wrote on last edited by
        #3

        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...

        J N 2 Replies Last reply
        0
        • S saud_a_k

          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...

          J Offline
          J Offline
          Jon G
          wrote on last edited by
          #4

          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 String

              myDataSet = 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 Try

          Okay, 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

          1 Reply Last reply
          0
          • S saud_a_k

            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 the DataGrid I want that cell to contain "NONE" as its value I wrote the code like

            if(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...

            N Offline
            N Offline
            Not Active
            wrote on last edited by
            #5

            Handle the ItemDataBound eventprivate 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"; } }

            S 1 Reply Last reply
            0
            • S saud_a_k

              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...

              N Offline
              N Offline
              Not Active
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • N Not Active

                Handle the ItemDataBound eventprivate 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"; } }

                S Offline
                S Offline
                saud_a_k
                wrote on last edited by
                #7

                Thanx guys... I think I can insert a new row like Mark says but the problem seems to be enlarging. But thanx anyways _____________________________________________________ Yea! I could be wrong...

                1 Reply Last reply
                0
                • S saud_a_k

                  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 the DataGrid I want that cell to contain "NONE" as its value I wrote the code like

                  if(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...

                  S Offline
                  S Offline
                  saud_a_k
                  wrote on last edited by
                  #8

                  I changed the query & I got it.. I just had to use left outer joins and the ISNULL(fiels, 'None')Field_Name, thassit... Thanx guys. _____________________________________________________ Yea! I could be wrong...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups