A
A
Alexander Kovalenko2011-08-25 22:15:52
PostgreSQL
Alexander Kovalenko, 2011-08-25 22:15:52

Working with DataGridView?

The question is the following (I wrote it off on the MSDN forum, while deafly), can someone tell me?
There are 2 tables: Categories, Products
The first table contains the index and name of category
1, Category #1
2, Category #2, etc.
The second, respectively, is the index, product name and category index from the first table, i.e. field types (in postgresql) — integer, char(64), smallint
For example:
1, Product #1, 1
2, Product #2, 1
3, Product #3, 2
using SQL query I fill in the dtsource table (DataTable type) and specify it as a source for the DataGridView
grid_devices.DataSource = dtsource;
, after which I get a table:
5356
but I would like to display a combo box with category names in the Category column
is it possible to change the current column type? Manually setting the CellType property did not work
In all the examples and solutions that I found, a new column was manually added to the DataGridView via Columns.Add
adding a column to the DataGridView will change the DataTable associated with it, and after the Update operation to make changes to the postgre database, there will be an error , related to the appearance of an additional column,
how to display the names of categories from the "Category" table instead of numbers with the possibility of changing them through the ComboBox?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AFC, 2011-09-20
@AFC

IMHO as you want - in any way. You need a specific column to be of the DataGridViewComboBoxColumn type, and since your columns are generated automatically, it is not possible to change it right away as a DataGridViewTextBoxColumn . Those. everything is correct - demolish the extra column and add the one you need with the correct mapping. (Actually, I don’t understand why changing the DataGridView view affects the data source, maybe you don’t say something?)
Or, if you want to shoot yourself in the foot, override the EditingControlShowing and CellPainting methods and draw whatever you want yourself. A good article on this topic:
www.rsdn.ru/article/dotnet/DataGridView20part2.xml

D
DartAlex, 2016-04-01
@DartAlex

I filled the DataGridView line by line

private void Form_Load(object sender, EventArgs e)
{
    dataGridView1.ColumnCount = 3;
    dataGridView1.Columns[0].Name = "ID";
    dataGridView1.Columns[1].Name = "Наименование";
    dataGridView1.Columns[2].Name = "Категория";
    dataGridView1.AllowUserToAddRows = false;
    dataGridView1.AllowUserToDeleteRows = false;

    string ConnStr = SQLCommands.ConnectionString();
    string Command = "SELECT * FROM [dbo].[Изделия]";
    DataTable Table1 = new DataTable();
    Table1 = SQLGetData(ConnStr, Command).Tables[0];

    foreach (DataRow row in Table1.Rows)
    {
         DataGridViewRow[] dgwrow = new DataGridViewRow[1];
         dgwrow[0] = GetComboBox();
         // Устанавливаем значения из таблицы изделия
         try
         {
             dgwrow[0].Cells[0].Value = row[0].ToString();
             dgwrow[0].Cells[1].Value = row[1].ToString();
             dgwrow[0].Cells[2].Value = row[2].ToString();
         }
         catch { }
         dataGridView1.Rows.Add(dgwrow[0]);
    }
}

private DataGridViewRow GetComboBox()
{
    string ConnStr = "ConnectionString";
    string Command = "SELECT [Name] FROM [dbo].[Категория]";
    DataTable DT = new DataTable();
    DT = SQLGetData(ConnStr, Command).Tables[0];

    DataGridViewRow roww = new DataGridViewRow();
    // создаём комбобокс
    DataGridViewComboBoxCell cell_CB = new DataGridViewComboBoxCell();
    roww.CreateCells(dataGridView1);
    // наполняем из таблицы "категория"
    foreach (DataRow row in DT.Rows)
    {
        Name = row["Name"].ToString();
        cell_CB.Items.AddRange(Name);
    }
    // третья колонка будет комбобокс
    roww.Cells[2] = cell_CB;
    return roww;
}

public static DataSet SQLGetData(string ConnectionString, string commandString)
{
    using (SqlConnection connection = new SqlConnection(ConnectionString))
    {
        DataSet DS = new DataSet();
        DataTable DT = new DataTable("Table1");
        try
        {
            connection.Open();
            SqlCommand command = new SqlCommand(commandString, connection);
            SqlDataReader read = command.ExecuteReader();
            DS.Tables.Add(DT);
            DS.Load(read, LoadOption.PreserveChanges, DS.Tables[0]);
                   
        }
        catch (SqlException e)
        {
            System.Windows.Forms.MessageBox.Show(e.Message);
        }
        finally
        {
            connection.Close();
        }
        return DS;
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question