• After more than 30 years running websites and forums I am retiring.

    I have made many friends through the years. I will cherish my time getting to know you. I wish you all the best. This was not an easy decision to make. The cost to keep the communities running has gotten to the point where it's just too expensive. Security certificates, hosting cost, software renewals and everything else has increased threefold. While costs are up ad revenue is down. It's no longer viable to keep things running.

    All sites will be turned off on Thursday 30 November 2023. If you are interested in acquiring any of the websites I own you can Email Schwarz Network.

Modify Type of Dataset Column

catlook

Newcomer
Joined
Jun 28, 2007
I'm working with a windows foem application. I have a dataset that was returned from a webservice. The third column ("Permanent") contains a bool. When i bind the data to a datagridview, that coulmn shows up as a series of checked checkboxes (since all the rows happened to have "true" for that value). What I want to do is modify the dataset, before binding it to the grid, to display the word "Permanent" if the bool is true and "Temporary" is the bool is false. I've gotten the code the point where I recognise whether it's true or false, but, because the column expects a bool, I can't put in the appropriate string. Any suggestions?


Code:
private void btnGetAccounts_Click(object sender, EventArgs e)
{
  BindingSource bs = new BindingSource();
  dsAccounts = new DataSet();
  dsAccounts = myAccount.List(nSessionID, out nError, out sErrorMsg);
  if (dsAccounts != null && dsAccounts.Tables.Count > 0)
  { 
    //modify datadisplay
    for (int i = 0; i < 10; i++)
    {
      if ((bool)dsAccounts.Tables[0].Rows[i][2] == true)
     {
       dsAccounts.Tables[0].Rows[i][2] = "Permanent";
     }
   }

  bs.DataSource = dsAccounts;
  bs.DataMember = dsAccounts.Tables[0].TableName; 
  dgvAccounts.DataSource = bs;
  }
}

The error I get is: "String was not recognised as valid Boolean. Couldn't store "Permanent" in Permanent column. Expected type is Boolean."

It has been suggested that I use the RowDataBound event, but, sa far as I know, that only applies to ASP. That event doesn't seem to be available for DataGridViews in windows forms applications.
 

davearia

Centurion
Joined
Jan 4, 2005
Hi,

You cannot modify the column type once you have data in there.

Here is what I would do, it's in vb as I am at work.
Visual Basic:
 Dim dtResults As New DataTable

For Each dc As DataColumn In dtOriginalTable.Columns
    dtResults.Columns.Add(dc.ColumnName)
Next

For Each dc As DataColumn In dtResults.Columns
    If dc.ColumnName = "columnToChange" Then
        dc.DataType = System.Type.GetType("System.String")
    End If
Next

For Each drow As DataRow In dtOriginalTable.Rows
    dtResults.Rows.Add(drow.ItemArray)
    If Convert.ToBoolean(dtResults.Rows(dtResults.Rows.Count - 1).Item("columnToChange")) Then
        dtResults.Rows(dtResults.Rows.Count - 1).Item("columnToChange") = "Permanent"
    Else
        dtResults.Rows(dtResults.Rows.Count - 1).Item("columnToChange") = "Temporary"
    End If
Next

So you bind to the new datatable and it should fine.
 

dliedke

Newcomer
Joined
May 3, 2012
Thanks a lot!! Great code, this is the version in C# only changing the column type:

DataTable dtOriginalTable = dataSet.Tables[0];

DataTable dtResults = new DataTable();
foreach (DataColumn dc in dtOriginalTable.Columns)
{
dtResults.Columns.Add(dc.ColumnName);
}

foreach (DataColumn dc in dtResults.Columns)
{
if (dc.ColumnName == "Incident Priority")
{
dc.DataType = System.Type.GetType("System.String");
}
}

foreach (DataRow drow in dtOriginalTable.Rows)
{
dtResults.Rows.Add(drow.ItemArray);
}
 
Top Bottom