Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8501459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:13:08+00:00 2026-06-11T01:13:08+00:00

Using C# Using SQL I populate a datatable with the results from a query.

  • 0

Using C#

Using SQL I populate a datatable with the results from a query.

then I created a datagrid that uses that datatable as its source.

The datagrid is already configured with a column of type DataGridViewComboBoxColumn.

The Combobox’s datasource is another SQL loaded datatable that has two strings for each row. One string is the actual value I need, the second string is the user visible string.

The combobox is populated correctly with the information from the 2nd datatable and the values work as desired. When I save the data out of the datatable I’m getting the correct information for the combobox.

The problem comes into play when I try loading the saved SQL information back into that datagrid. The value doesn’t seem to get bound to item the datatable.

Datagrid definition:

        dgHeaders.DataSource = dtCSVHeaders;

        dgHeaders.Columns["nIndex"].Visible = false;

        if (!dgHeaders.Columns.Contains("colType"))
        {
            DataGridViewComboBoxColumn colType = new DataGridViewComboBoxColumn();
            colType.HeaderText = "DB Field";
            colType.DropDownWidth = 140;
            colType.Width = 140;
            colType.DataSource = dtFields;
            colType.DataPropertyName = "szDBField";
            colType.DisplayMember = "szVisualField";
            colType.ValueMember = "szDBField";
            colType.Name = "DB Field";
            colType.DefaultCellStyle.NullValue = "--Select--";
            dgHeaders.Columns.Add(colType);

        }

First I tried to set the column name colType to the value I was populating in the dataRow but that didn’t seem to work.

Since I couldn’t figure out how to get the databinding to work, I decided I’d try to force the cell to the value I wanted so I tried this on the dataload.

        ... SQL load

        foreach (DataRow drRow in dtLoad.Rows)
        {
            string szColumnName = drRow["szOrgColumnName"].ToString();
            string szMatchName = drRow["szMatchColumn"].ToString();
            DataRow drAdd = dtCSVHeaders.NewRow();
            drAdd["nIndex"] = nLoop;
            drAdd["szHeader"] = szColumnName;
            dtCSVHeaders.Rows.Add(drAdd);
            dgHeaders.Rows[nLoop].Cells[2].Value = szMatchName;
            nLoop++;
        }

But sadly that still doesn’t set the value of the combobox.

I’ve got no errors or warnings on this code.

I’d prefer to let databinding take control and do its thing without me specifically setting the cell with the value. But if that’s what I need to do then so be it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:13:09+00:00Added an answer on June 11, 2026 at 1:13 am

    Hmm, your code (as far as I seen) seem good.
    I dont know what exactly is your “dtFields” object, but it should be dataTable.
    I did a simple example of how it should be done.
    Check it out:

    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            dataGridView1.Columns.Add("column1", "Column name");
            dataGridView1.Columns.Add(CreateComboBox());
            //adding some rows:
            dataGridView1.Rows.Add("a");
            dataGridView1.Rows.Add("b");
        }
    
        private DataGridViewComboBoxColumn CreateComboBox()
        {
            DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
            {
                combo.Name = "comboColumn";
                combo.HeaderText = "Selection";
                combo.DataSource = GetDataForComboBox();
                combo.DisplayMember = "Name";
                combo.ValueMember = "Id";
            }
            return combo;
        }
    
        private DataTable GetDataForComboBox()
        {
            //this is your method to get the data from database
    
            //in my exmaple I will simply add some example data:
            DataTable table = new DataTable("ExampleData");
            table.Columns.Add("Id", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Rows.Add(1, "Name 1");
            table.Rows.Add(2, "Name 2");
            table.Rows.Add(3, "Name 3");
            return table;
        }
    }
    

    This code works, and try to do the same, you only fill dataTable from dataBase. But if you want you can only try this exact code of mine, and you will see it work!

    bye

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2005 I have a query that gets child records from bundles
Am having a combobox which has its data populated from a SQL database, using
i have a method that will populate a datatable with the data from a
I have worked on retrieving the data from SQL database using Datareader and populate
I'm pasting test data from a SQL results into my code. I'm using the
iv created a database with some tables and populated them using SQL server 2008,
Using SQL Server 2000 My Query. SELECT (Format(IIf(CLng(OutTime) > 180000, CDate('18:00:00'), CDate(Format(OutTime, '00:00:00'))) -
I´m using SQL Server 2005 and Visual Studio 2008, C#. In the data source
I am using Linq-To-Sql to populate my business layer. Here is a snippet of
I'm using VB.NET. I am performing a select query that returns approximately 2500 rows,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.