Hi, Im tyring to display a dataGridView with comboBoxes on about 5 of the fields. This table is basically permissions to certain parts of my program. In the database i just store a 1 or a 0 (access or no access). Now obviously to the user i want this to be displayed as text. Now currently i am creating the columns manually and using the cell changed event to do the updating. This is quite long winded and if possible i would like a simpler (and cleaner) way to do this.
Heres a snippet of code for adding the comboBox Columns:
And heres a snippet of the updating code:
So, the things i want to be able to do are: value already in database is displayed in text form when loading, On the value changing update the database value to 1 and having the text values in the ddl not the int values.
If theres not a simpler way to do this can someone please tell me the best way to set the values for each row... Because currently theres a datasource binded to the dgv and i just add the other columns on (hence the DisplayIndex property).
Many Thanks,
Regards,
Piercy
Heres a snippet of code for adding the comboBox Columns:
Code:
DataGridViewComboBoxColumn ddlConfig = new DataGridViewComboBoxColumn();
ddlConfig.HeaderText = "Config";
ddlConfig.Items.Add("No Access");
ddlConfig.Items.Add("Full Access");
ddlConfig.DisplayIndex = 2;
dataGridView1.Columns.Add(ddlConfig);
Code:
string primKey = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Config" ||
dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Reports" ||
dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Groups" ||
dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Predefined" ||
dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() == "Permissions")
{
int val = 0;
if (dataGridView1.CurrentCell.Value.ToString() == "No Access")
val = 0;
else if (dataGridView1.CurrentCell.Value.ToString() == "Full Access")
val = 1;
else
val = 0;
string command = "UPDATE Permissions SET " + dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText.ToString() + "='" + val + "' WHERE ID = " + primKey;
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(command, conn);
cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
conn.Close();
}
If theres not a simpler way to do this can someone please tell me the best way to set the values for each row... Because currently theres a datasource binded to the dgv and i just add the other columns on (hence the DisplayIndex property).
Many Thanks,
Regards,
Piercy