0

I have a table as following in SQL Database. Devicereg Table.

  No   |    Parameter | DataTyp  |  Enable  | 
  1            xxxx      Int         True
  2            yyyy      Int         True
  3            tttt      String      False

I want to show these data in DataGridView and its DataTyp column want to add Combobox with default value table cell value, Enable column want to add a checkbox with default value table cell value.

Combobox want to add the following list and the default value is one of following value.

  • Int
  • String
  • Floart

Following code, Combobox value adds all columns value in one combo box.

Code:

    string connetionString = null;
        SqlConnection connection;
        SqlDataAdapter adapter = new SqlDataAdapter();
        string sql = null;
        bool st = false;

        DataSet ds = new DataSet();
        connection = new SqlConnection(connetionString);
        sql = "select * from Devicereg";
            try
            {
                connection.Open();
                adapter.SelectCommand = new SqlCommand(sql, connection);
                adapter.Fill(ds);
                connection.Close();
                dataGridView1.DataSource = null;
                dataGridView1.ColumnCount = 0;
         
                dataGridView1.DataSource = ds.Tables[0];

                DataGridViewComboBoxColumn dc = new DataGridViewComboBoxColumn();
                dc.DataSource = ds.Tables[0];
                dc.ValueMember = "Datatyp";

                dataGridView1.Columns.Add(dc);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

Example Photo:

enter image description here

Edit 1:

I have done it but how to display only defined items in the dropdown.

Code:

DataGridViewComboBoxColumn dc = new DataGridViewComboBoxColumn();
dc.DataSource = ds.Tables[0];
dc.DataPropertyName = "Datatyp";
dc.ValueMember = "Datatyp";
dc.DisplayMember = "Datatyp";

I have 36 rows and all rows Datatyp values shows. I want specific items to select like Int, Flort, Strings only.

Output:

enter image description here

Edit 2:

If I set like the following code, I got error message.

DataGridViewComboBoxColumn dc = new DataGridViewComboBoxColumn();
dc.DataSource = new List<string> { "Int", "String", "Flort" };
dc.DataPropertyName = "Datatyp";
dc.ValueMember = "Datatyp";
dc.DisplayMember = "Datatyp";

Error:

enter image description here

saji indra
  • 57
  • 1
  • 1
  • 9
  • Not quite clear your needs. You can set the dc's DataSource like `dc.DataSource = new List { "Int", "String", "Float" };`. – 大陸北方網友 Jul 20 '20 at 05:44
  • @KyleWang I need to put check box and combo box to 3rd and 4th columns instead of just displaying the values. Combo box want to add some more items to select. Those are int, string, float. – saji indra Jul 20 '20 at 06:50

1 Answers1

2

To import the DataTyp from database to DataGridViewComboBoxColumn, please refer to the following steps.

First, add the columns to datagridview. Set the DataTyp's columntype to DataGridViewComboBoxColumn, and set its DataSource like:

enter image description here

Next, set Enable columntype to DataGridViewCheckBoxColumn.

Or via code:

var colNo = new DataGridViewTextBoxColumn
{
    HeaderText = "No",
    Name = "No"
};
var colParameter = new DataGridViewTextBoxColumn
{
    HeaderText = "Parameter",
    Name = "Parameter"
};
var colDataTyp = new DataGridViewComboBoxColumn
{
    HeaderText = "DataTyp",
    Name = "DataTyp",
    DataSource = new List<string> { "Int", "String", "Float" }
};
var colEnable = new DataGridViewCheckBoxColumn
{
    HeaderText = "Enable",
    Name = "Enable"
};

dataGridView1.Columns.AddRange(new DataGridViewColumn[] { colNo, colParameter, colDataTyp, colEnable });

Then fill the datagridview via the code below.

DataSet ds;
string connetionString = @"Connection String";
using (SqlConnection conn = new SqlConnection(connetionString))
{
    SqlDataAdapter sda = new SqlDataAdapter("Select * From Devicereg", conn);
    ds = new DataSet();
    sda.Fill(ds, "T1");
}

DataGridViewComboBoxCell typeCell;

foreach (DataRow row in ds.Tables[0].Rows)
{
    int index = dataGridView1.Rows.Add();
    dataGridView1.Rows[index].Cells["No"].Value = row[0];
    dataGridView1.Rows[index].Cells["Parameter"].Value = row[1];

    typeCell = (DataGridViewComboBoxCell)(dataGridView1.Rows[index].Cells["DataTyp"]);
    typeCell.Value = row[2].ToString().Trim();

    dataGridView1.Rows[index].Cells["Enable"].Value = row[3];
}

The result,

enter image description here

大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • Data set have Database, Service and Object Which one? I want to do it by the program because it is changeing by a button click. – saji indra Jul 20 '20 at 08:24
  • @sajiindra `I want to do it by the program because it is changeing by a button click.` Could you explain this sentence in detail? You don't need to add project data source in this demo. – 大陸北方網友 Jul 20 '20 at 08:26
  • I want to add another five more tables to this by buttons but columns are different. Other tables also want this checkbox and combobox. I want to do by the program to add columns. – saji indra Jul 20 '20 at 08:37
  • 'Set its DataSource' how to do it ? It has Database, Service and Object. – saji indra Jul 20 '20 at 08:39
  • If you have many tables with different columns, you need to get the column names via [sql statement](https://stackoverflow.com/questions/1054984/how-can-i-get-column-names-from-a-table-in-sql-server). I have updated the answer that add columns by coding. I don’t quite understand why you must "set DataSource" – 大陸北方網友 Jul 20 '20 at 08:44
  • @sajiindra Do you mean that set "DataGridViewComboBoxColumn" source? It is set in `Edit Columns` popup, rather than the designer. As shown in the picture above, it is set in the "Items" property. – 大陸北方網友 Jul 20 '20 at 08:50
  • Thank you very much, Why DataPropertyName, ValueMember etc not using instead of foreach loop? Please do not edit above answer if you put code. put new answer. Why it is not working? See Edit 1 and 2 – saji indra Jul 20 '20 at 09:03
  • You need to assign data to the DataTyp column of each row, instead of just setting the data source. – 大陸北方網友 Jul 20 '20 at 09:08
  • Edit 1: You got all the DataTyp value as the data source, so it will display all. Maybe you can use key word `DISTINCT` to remove duplicates. Edit 2: You set a list as the data source, and it doesn't contain the filed "DataTyp". – 大陸北方網友 Jul 20 '20 at 09:19
  • @sajiindra If this answer helps you, please click `√` to [mark it as answer](https://stackoverflow.com/help/someone-answers). – 大陸北方網友 Jul 20 '20 at 09:20
  • Any method using DataPropertyName, ValueMembe and DataSource to work Edit 2? Any simple idea? Why does not contain filed Datatype? – saji indra Jul 20 '20 at 09:30