combobox как получить первое значение при добавление данных в бд
в combobox вывел значение id и name из бд. значение name отображается в Datagridview, с помощью dataGridView_CellMouseClick вывожу текущее значение в combobobx, мне нужно храниьи код вывода данных из бд в combobox
void SelectSatsFromSum()
{
MySqlDataAdapter adapterr = new MySqlDataAdapter("SELECT satstype.id AS id, CONCAT(satstype.id, '|', satstype.name) as name FROM satstype", con);
DataSet dsett = new DataSet();
adapterr.Fill(dsett);
comboBox1.ValueMember = "id";
comboBox1.DisplayMember = "name";
comboBox1.DataSource = dsett.Tables[0];
comboBox1.Text = "SELECT name";
}
код хранение в базу данных
cmdUpdate.Parameters.Add("@satstypeID", MySqlDbType.VarChar, 11).Value = comboBox1.Text; // тут нужно хранить id, у меня храниться name
код вывода значения из datagridview в combobox
comboBox1.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
CREATE TABLE `Region` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`satstypeID` int(11) DEFAULT NULL,
`yearID` int(11) DEFAULT NULL,
`prefix` varchar(50) NOT NULL,
`size` int(20) NOT NULL,
`freeSize` int(20) NOT NULL,
`internetSize` int(20) NOT NULL,
`freeSizeInt` int(20) NOT NULL,
`withoutIntNumber` int(20) NOT NULL,
`kabel10` int(20) NOT NULL,
`kabel20` int(20) NOT NULL,
`kabel30` int(20) NOT NULL,
`kabel50` int(20) NOT NULL,
`kabel100` int(20) NOT NULL,
`kabel200` int(20) NOT NULL,
`dizelID` int(11) DEFAULT NULL,
`batteryStatus` varchar(20) NOT NULL,
`batterygroupID` int(11) DEFAULT NULL,
`montyor` varchar(20) NOT NULL,
`geolocationc` varchar(20) NOT NULL,
`geolocationb` varchar(20) NOT NULL,
`location` varchar(100) NOT NULL,
`osak` int(20) NOT NULL,
`image` longblob NOT NULL,
`SummaID` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `satstypeID` (`satstypeID`),
KEY `yearID` (`yearID`,`batterygroupID`),
KEY `batterygroupID` (`batterygroupID`),
KEY `dizelID` (`dizelID`),
KEY `regionID` (`SummaID`),
CONSTRAINT `regionBatterygroup` FOREIGN KEY (`batterygroupID`)
REFERENCES `batterygroup` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `regionDizelID` FOREIGN KEY (`dizelID`) REFERENCES `dizel`
(`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `regionSatsTypeID` FOREIGN KEY (`satstypeID`) REFERENCES
`satstype` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `regionSummaID` FOREIGN KEY (`SummaID`) REFERENCES `summa`
(`id`) ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT `regionYearID` FOREIGN KEY (`yearID`) REFERENCES `year`
(`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
таким образом вставил
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
SelectItemByDisplayMember(comboBox1, dataGridView1.CurrentRow.Cells[2].Value.ToString(), true);
SelectItemByDisplayMember(comboBox2, dataGridView1.CurrentRow.Cells[3].Value.ToString());
maskedTextBox1.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
textBox5.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
textBox6.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
textBox7.Text = dataGridView1.CurrentRow.Cells[9].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[22].Value.ToString();
textBox9.Text = dataGridView1.CurrentRow.Cells[17].Value.ToString();
textBox10.Text = dataGridView1.CurrentRow.Cells[10].Value.ToString();
textBox11.Text = dataGridView1.CurrentRow.Cells[11].Value.ToString();
textBox12.Text = dataGridView1.CurrentRow.Cells[12].Value.ToString();
textBox13.Text = dataGridView1.CurrentRow.Cells[13].Value.ToString();
textBox14.Text = dataGridView1.CurrentRow.Cells[14].Value.ToString();
textBox15.Text = dataGridView1.CurrentRow.Cells[15].Value.ToString();
SelectItemByDisplayMember(comboBox4, dataGridView1.CurrentRow.Cells[16].Value.ToString());
textBox16.Text = dataGridView1.CurrentRow.Cells[20].Value.ToString();
textBox17.Text = dataGridView1.CurrentRow.Cells[21].Value.ToString();
// comboBox3.Text = dataGridView1.CurrentRow.Cells[25].Value.ToString();
textBox18.Text = dataGridView1.CurrentRow.Cells[19].Value.ToString();
textBox20.Text = dataGridView1.CurrentRow.Cells[23].Value.ToString();
SelectItemByDisplayMember(comboBox7, dataGridView1.CurrentRow.Cells[18].Value.ToString());
}
код вывода данных из бд в Datagridview
public void viewDatagridview()
{
con.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT region.id, region.name, satstype.name, year.name, region.prefix, region.size, region.freeSize, region.internetSize, region.freeSizeInt, region.withoutIntNumber, region.kabel10, region.kabel20, region.kabel30," +
" region.kabel50, region.kabel100, region.kabel200, dizel.name, region.batteryStatus, batterygroup.name AS 'batterygroup', region.montyor, region.geolocationc, region.geolocationb, region.location, region.osak FROM region LEFT JOIN satstype ON region.satstypeID = satstype.id" +
" LEFT JOIN year ON region.yearID = year.id LEFT JOIN batterygroup ON region.batterygroupID = batterygroup.id LEFT JOIN dizel ON region.dizelID = dizel.id", con);
DataSet dset = new DataSet();
adapter.Fill(dset);
dataGridView1.DataSource = dset.Tables[0];
con.Close();
}
Ответы (1 шт):
Автор решения: Konst
→ Ссылка
так как первый комбобокс содержит комплексное отображаемое значение, то поиск по Contains:
private void SelectItemByDisplayMember(ComboBox cb, string value, bool contains = false)
{
for (int i = 0; i < cb.Items.Count; i++)
{
var prop = cb.Items[i].GetType().GetProperty(cb.DisplayMember);
if (prop != null)
{
bool find;
if (contains)
{
find = prop.GetValue(cb.Items[i], null).ToString().Contains(value);
}
else
{
find = prop.GetValue(cb.Items[i], null).ToString() == value;
}
if (find)
{
cb.SelectedIndex = i;
break;
}
}
}
}
public static void CellMouseClickk(int id, TextBox textBox1, MaskedTextBox maskedTextBox1, TextBox textBox2, TextBox textBox3, TextBox textBox4, TextBox textBox5, TextBox textBox6, TextBox textBox7, TextBox textBox9, TextBox textBox10, TextBox textBox11, TextBox textBox12, TextBox textBox13, TextBox textBox14, TextBox textBox15, TextBox textBox16, TextBox textBox17, TextBox textBox18, TextBox textBox20, ComboBox comboBox1, ComboBox comboBox2, ComboBox comboBox3, ComboBox comboBox4, ComboBox comboBox7, DataGridView dataGridView1)
{
id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString());
textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
SelectItemByDisplayMember(comboBox1, dataGridView1.CurrentRow.Cells[2].Value.ToString(), true);
SelectItemByDisplayMember(comboBox2, dataGridView1.CurrentRow.Cells[3].Value.ToString());
maskedTextBox1.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
textBox5.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
textBox6.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
textBox7.Text = dataGridView1.CurrentRow.Cells[9].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[22].Value.ToString();
textBox9.Text = dataGridView1.CurrentRow.Cells[17].Value.ToString();
textBox10.Text = dataGridView1.CurrentRow.Cells[10].Value.ToString();
textBox11.Text = dataGridView1.CurrentRow.Cells[11].Value.ToString();
textBox12.Text = dataGridView1.CurrentRow.Cells[12].Value.ToString();
textBox13.Text = dataGridView1.CurrentRow.Cells[13].Value.ToString();
textBox14.Text = dataGridView1.CurrentRow.Cells[14].Value.ToString();
textBox15.Text = dataGridView1.CurrentRow.Cells[15].Value.ToString();
SelectItemByDisplayMember(comboBox4,dataGridView1.CurrentRow.Cells[16].Value.ToString());
textBox16.Text = dataGridView1.CurrentRow.Cells[20].Value.ToString();
textBox17.Text = dataGridView1.CurrentRow.Cells[21].Value.ToString();
// comboBox3.Text = dataGridView1.CurrentRow.Cells[25].Value.ToString();
textBox18.Text = dataGridView1.CurrentRow.Cells[19].Value.ToString();
textBox20.Text = dataGridView1.CurrentRow.Cells[23].Value.ToString();
SelectItemByDisplayMember(comboBox7, dataGridView1.CurrentRow.Cells[18].Value.ToString());
}
при добавлении выбранного в базу:
cmdUpdate.Parameters.AddWithValue("@satstypeID", comboBox1.SelectedValue);
событие клика на ячейку в гриде:
public partial class Form1 : Form
{
private void Grid_CellMouseClick(Object sender, DataGridViewCellMouseEventArgs e)
{
MessageBox.Show("Mouse clicked in the datagridview!");
}
public Form1()
{
InitializeComponent();
dataGridView1.CellMouseClick += Grid_CellMouseClick;
...
}
...
}


