5 Replies - 47468 Views - Last Post: 21 May 2010 - 04:58 AM Rate Topic: -----

#1 mishipal  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 04-May 09

Set Datagridview Combobox Cell Value Programmatically from database

Posted 21 May 2010 - 03:35 AM

I have a datagrid with first coloumn as combobox and second one as textbox.
The combobox had datasource with displaymember and valuemember.

The problem is when i try to retrieve value from database and put it into the grid i get exception

System.FormatException : DatagridviewComboBoxCell value is not valid

I set the value using

For i = 0 To Dt_Edit.Rows.Count - 1
   Try
     Dg.Rows(i).Cells("txtAmount").Value = Dt_Edit.Rows(i)("Amount").ToString
     CType(Dg.Rows(i).Cells("cmbAccount"), DataGridViewComboBoxCell).Value = CInt(Dt_Edit.Rows(i)("AcId").ToString)
   Catch ex As Exception
   End Try
Next



I have also tried
dg.rows(i).cells("cmbAccount").value = Dt_Edit.Rows(i)("AccountName").ToString()



but no luck.....

Please Help

Is This A Good Question/Topic? 0
  • +

Replies To: Set Datagridview Combobox Cell Value Programmatically from database

#2 mr.coder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 41
  • Joined: 31-January 10

Re: Set Datagridview Combobox Cell Value Programmatically from database

Posted 21 May 2010 - 03:46 AM

Could you elaborate your code? It would help to get a better idea on your problem!
Was This Post Helpful? 0
  • +
  • -

#3 mishipal  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 04-May 09

Re: Set Datagridview Combobox Cell Value Programmatically from database

Posted 21 May 2010 - 03:51 AM

The Datatable "Dt_Edit" had 2 coloumns "AcId" and "Amount" which is populated from database.

The datagridview has two coloumns :

1. cmbAccounts - Datagridviewcombobox
2. txtAmount - Datagridviewtextbox

Then i enter the values from Datatable into the grid

This post has been edited by mishipal: 21 May 2010 - 03:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 mr.coder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 41
  • Joined: 31-January 10

Re: Set Datagridview Combobox Cell Value Programmatically from database

Posted 21 May 2010 - 03:55 AM

View Postmishipal, on 21 May 2010 - 03:21 PM, said:

i am setting values fetched by Datatable Dt_Edit one by one into Datagrid


Post out your code snippet from the part where you connect your program to a Database. It would be helpful to understand the problem!
Was This Post Helpful? 0
  • +
  • -

#5 mishipal  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 04-May 09

Re: Set Datagridview Combobox Cell Value Programmatically from database

Posted 21 May 2010 - 04:06 AM

The Combobox is populated by this code
Sub Sub_Get_Accounts()

        Dim Sqlconn As New SqlConnection("Data Source=SERVER;Initial Catalog=LMS;Persist Security Info=True;User ID=sa;Password=sa;")
        Dim Da As SqlDataAdapter

        Try
            Sqlconn.Open()                          'open connection to sql
            DtAccounts = New DataTable
            Da = New SqlDataAdapter("SELECT AccountId,AccountName, GroupID From TblAccountMaster order by AccountName", Sqlconn)

            Da.Fill(DtAccounts)

            cmbAccount.DataSource = DtAccounts
            cmbAccount.DisplayMember = "AccountName"
            cmbAccount.ValueMember = "AccountId"

            Sqlconn.Close()                         'Close Sql Connection
            Sqlconn.Dispose()                       'Dispose Sql Connection
        Catch ex As Exception
            'Me.Message.Text = ex.Message
        End Try
    End Sub



The Data is populated and added to data to grid by following code
Dim Sqlconn As New SqlConnection("Data Source=SERVER;Initial Catalog=LMS;Persist Security Info=True;User ID=sa;Password=sa;")
Dim Da As SqlDataAdapter
Dim SqlQuery As String

SqlQuery = "Select a.AcId, a.Amount From TblGetValue a WHERE a.VoucherId = 10 "

 Try
    Sqlconn.Open()
    Da = New SqlDataAdapter(SqlQuery, Sqlconn)
    Dt_Edit = New DataTable
    Da.Fill(Dt_Edit)
    
    Dg.Rows.Add(Dt_Edit.Rows.Count)

    For i = 0 To Dt_Edit.Rows.Count - 1
       Try
         Dg.Rows(i).Cells("txtAmount").Value = Dt_Edit.Rows(i)("Amount").ToString
         Dg.Rows(i).Cells("cmbAccount").Value = Dt_Edit.Rows(i)("AcId").ToString
       Catch ex As Exception
       End Try
    Next
    
    Dg.Visible = True

        Catch ex As Exception
            MessageBox.Show("Could not details of Voucher", "Error in Sub_Get_Details", MessageBoxButtons.OK)
        End Try
    End Sub



Was This Post Helpful? 0
  • +
  • -

#6 mr.coder  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 41
  • Joined: 31-January 10

Re: Set Datagridview Combobox Cell Value Programmatically from database

Posted 21 May 2010 - 04:58 AM

Since you are using a ComboBox, you cannot use the .Value Property to add items in a ComboBox.

You can proceed with your code as:


    Dim CBCell As DataGridViewComboBoxCell
    For i = 0 To Dt_Edit.Rows.Count - 1
       Try
         Dg.Rows(i).Cells("txtAmount").Value = Dt_Edit.Rows(i)("Amount").ToString
         
         CBCell = DataGridView1.Rows(0).Cells("cmbAccount")
         CBCell.Items.Add(Dt_Edit.Rows(i)("AcId").ToString)

       Catch ex As Exception
       End Try
    Next




Hope it helps!

This post has been edited by mr.coder: 21 May 2010 - 05:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1