Code Q & A
  • index
  • Java
  • python
  • javascript
No Result
View All Result
Code Q & A
  • index
  • Java
  • python
  • javascript
No Result
View All Result
Code Q & A
No Result
View All Result

SelectedValue for ComboBox not getting set

06/23/2022
in c#, winforms
Reading Time: 2 mins read

I have comboBox name as cmbContactType. I binded this combo with dataSource as DisplayMember ValueName(string type) and valueMember as ValueID(number type). When user change value in cmbContactType then cmbContactType.SelectValue get set and I am able to save document with this Selectedvalue. I facing problem while setting cmbContactType value from database which is of type number. Every time I am trying to set value, null value set to the cmbContactType.SelectValue.
Following is the code by which I am binding datasource to cmbContactType and for setting SelectedValue of cmbContactType. I am using VS2010 and MS-Access. Please help.

    //method to bind dataSource.
    public static void BindDataSourceWithCombo(ref ComboBox cmb, string query, string valueMember, string displayMember)
    {
        DataTable _tableSource = (new AccessConnectionManager()).GetDataTableBySQLQuery(query);

        var _dataSource = (from DataRow _row in _tableSource.Rows
                           select new
                           {
                               ValueMember = _row[valueMember],
                               DisplayMember = _row[displayMember].ToString()

                           }).ToList();

        cmb.DisplayMember = "DisplayMember";
        cmb.ValueMember = "ValueMember";
        cmb.DataSource = _dataSource;
    }

    // Method to set values in document.
    public void SetDocumentProperties()
    {
        string _query = string.Format("Select ContactCode,ContactName,ContactType from ContactMaster where ContactCode = '{0}'", Convert.ToString(cmbContactCode.Text));
        DataTable _table = AccessConnectionManagers.GetDataTableBySQLQuery(_query);

        if (_table.Rows.Count > 0)
        {
            DataRow _row = _table.Rows[0];
            txtContactCode.Text = Convert.ToString(_row["ContactCode"]);
            txtContactName.Text = Convert.ToString(_row["ContactName"]);
            cmbContactType.SelectedValue = _row["ContactType"];
        }
        else
        {
            txtContactCode.Text = string.Empty;
            txtContactName.Text = string.Empty;
            cmbContactType.SelectedValue = 1;
        }
    }

Here is code by which I bind dataSource with cmbConactType. This method call on Form_Load event.

    private void LoadContactTypeCombo()
    {
        //Table: PicklistValues(ID,MasterID,ValueID,ValueName) 
        string _query = string.Format("select ValueID,ValueName from PicklistValues where MasterID = {0}", 1);
        BindDataSourceWithCombo(ref cmbContactType, _query, "ValueID", "ValueName");
    }

try by this method

int index;
// Search the Item that matches the string
index=cmbContactType.FindString(_row["ContactType"]);
// Select the Item in the Combo
cmbContactType.SelectedIndex=index;

You should notice the type of the actual ValueMember in your DataSource, when assign the SelectedValue to some value, it should be castable to the type of the actual ValueMember in your DataSource.

You have to ensure that _row["ContactType"] has to be contained by the item values of your cmbContactType. If not, the SelectedItem will be null. You should perform some check and see if it helps.

Finally I found solution to my question, when I look for Type of cmbContactType.SelectedValue, I found it is typeof Int16...! and by using following statement cmbContactType.SelectedValue get set.

cmbContactType.SelectedValue =_row["ContactType"] != null ? Convert.ToInt16(_row["ContactType"]) : (Int16)(-1) ;

Tags: c#winforms
ShareTweetPin

Related Posts

asp.net

Websocket client not connecting to websocket server

I have a NET Core 3.1 API in which I create a websocket server and start it when the API...

.net-core

Pinging Websocket server with Websocket .NET client

I'm trying to figure out how to send ping/pong heartbeats in order to be sure to maintain a connection with...

asp.net

An invalid character was found in the mail header: ';'

error on this line "message.To.Add(strCommandText);" when I try to take the email data from the database to send an email....

asp.net-core

ASP.NET Core return JSON with status code

I'm looking for the correct way to return JSON with a HTTP status code in my .NET Core Web API...

Next Post

ComboBox Selection

Hot tags

.htaccess .net Algorithm amazon-web-services android Apache c# css c语言 django django-models docker google-app-engine google-cloud-platform hadoop Hive html ios java javascript kubernetes linux macos maven mysql node.js php postgresql python python-2-7 Python 3.X R ruby ruby-on-rails spring SQL SQL SERVER svn TensorFlow ubuntu windows xcode 爬虫 算法 编程语言

    © 2022 Websitedic.com.

    No Result
    View All Result
    • index
    • Java
    • python
    • javascript