grid_devices.DataSource = dtsource;
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;
}
}