locked
How to modify column data in DataSet before binding data RRS feed

  • Question

  • User886782413 posted

    Hi there,

    I’ve a table with column called A of type tinyint and it stores integers.

    In a C# page, I’m using the following code to dump this table:

    SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);

    DataSet myDataSet = new DataSet();

    mySqlAdapter.Fill(myDataSet);

    grdvRG.DataSource = myDataSet.Tables[0];

    grdvRG.DataBind();

    I can see the table. Before I do databind, I would like to change the column to strings such as this

    I would like to modify Column A if 1 = “Apple”, 2 = “Grapes”, 3 = “Banana”, etc.

    Can someone tell me is this is possible and how to do it.

    Thanks,

    Joe Green

    Monday, January 28, 2013 11:38 AM

Answers

  • User1690870723 posted

    That's fine, you would still use the rowdatabound event

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx 

     void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
    
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Display the company name in italics.
          string cellValue = e.Row.Cells[1].Text;
    
          switch(cellValue)
          {
            case "1":
               e.Row.Cells[1].Text = "Apple";
            ///etc ....
          }
    
        }
    
      }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 28, 2013 2:37 PM

All replies

  • User1690870723 posted

    You can use the RowDataBound event to change your column names based on the header value 

    http://www.dotnetfunda.com/forums/thread6313-how-to-change-gridview-column-name.aspx

    void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    
     {
    
               if (e.Row.RowType == DataControlRowType.Header)
    
                {
    
                    if (e.Row.Cells[0].Text = "1")
                    {
                      //do something
    
                    }
               }
    
    }



    Monday, January 28, 2013 12:03 PM
  • User886782413 posted

    I do not want to change column Name but the column "values".

    Column is titled "A". In this column "A", If

    1 ->Apple

    2 -> Grapes

    3 -> Banana

    Monday, January 28, 2013 1:28 PM
  • User1690870723 posted

    That's fine, you would still use the rowdatabound event

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx 

     void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
    
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Display the company name in italics.
          string cellValue = e.Row.Cells[1].Text;
    
          switch(cellValue)
          {
            case "1":
               e.Row.Cells[1].Text = "Apple";
            ///etc ....
          }
    
        }
    
      }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 28, 2013 2:37 PM