Combobox default value is always SQL table value - c# - c#

I am retrieving a datatable from a Stored Procedure and a Combobox is showing that data but I want to show -Select Program- as default value of Combobox but it is always showing the first record of datatable.
InitializeComponent();
cbxProgram.Items.Insert(0, "-SELECT PROGRAM-");
cbxProgram.SelectedIndex = 0;
cbxProgram.DataSource = SomeBL.GetProgramName().Tables[0];
cbxProgram.DisplayMember = "ProgramName";
cbxType.DataSource = SomeBL.GetType("").Tables[0].DefaultView;
cbxType.DisplayMember = "Type";
cbxNumber.DataSource = SomeBL.GetNumber("", "").Tables[0].DefaultView;
cbxNumber.DisplayMember = "Number";
cbxType.Enabled = false;
cbxType.Enabled = false;

You have to add your custom Row to your Table before binding.
I didn't test this, but it should look something like this:
DataTable dt = SomeBL.GetProgramName().Tables[0];
DataRow dr = dt.NewRow();
dr[0] = "-SELECT PROGRAM-"; // Look at the index of your desired column
dt.Rows.InsertAt(dr,0); // Insert on top position
cbxProgram.DataSource = dt;
cbxProgram.SelectedIndex = 0;
cbxProgram.DisplayMember = "ProgramName";

By using Datasource property, you delete the data exist in the first place. You should insert "-SELECT PROGRAM-" line after you fill the combobox from datasource.
Try this:
cbxProgram.DataSource = SomeBL.GetProgramName().Tables[0];
cbxProgram.DisplayMember = "ProgramName";
cbxProgram.Items.Insert(0, "-SELECT PROGRAM-");
cbxProgram.SelectedIndex = 0;

If it doesn't let you add another line after setting datasource, you may consider adding the line to the source datatable. (I don't recommend adding it from SQL database by changing your stored procedure.)
Check this pseudocode:
SomeBL.GetProgramName().Tables[0].Rows.Add(new DataRow(0, "-SELECT PROGRAM-"));
cbxProgram.DisplayMember = "ProgramName";
cbxProgram.Items.Insert(0, "-SELECT PROGRAM-");
cbxProgram.SelectedIndex = 0;

Related

Add default item to ComboBox while binding data with DataSet - WinForm

My Code to Bind ComboBox in windows forms is like this
ddlUsers.DataSource = dsUsers.Tables[0];
ddlUsers.ValueMember = "userID";
ddlUsers.DisplayMember = "Username";
I want to add a deafult Item as first element to this ComboBox, I have tried something like this
ddlUsers.Items.Insert(0, "-Select a user-");
But it throws an error like this.
Items collection cannot be modified when the DataSource property is set.
Can anyone point out correct approach to achieve this?
You have to add row in table this way:
DataRow newRow = dataSet1.Tables[0].NewRow();
newRow["userID"] = 0;
newRow["Username"] = "-Select a User-";
dataSet1.Tables[0].Rows.Add(newRow);
and then give source to combo:
ddlUsers.DataSource = dsUsers.Tables[0];
ddlUsers.ValueMember = "userID";
ddlUsers.DisplayMember = "Username";
For more information refer this MSDN article
Please try this :
DataTable dt = (DataTable)cmbControl.DataSource;
DataRow dr = dt.NewRow();
dr["UserName"] = "---Select User----";
dr["UserID"] = "0";
dt.Rows.InsertAt(dr, 0);
Insert row into datatable and use method InsertAt
You could insert the content of dsUsers.Tables[0] to a List first, add your manual item and then bind that List as DataSource instead of dsUsers.Tables[0] directly.
Insert a new Row at the top.
dr = dsUsers.Tables[0].NewRow();
dr[userID] = "0";
dr[Username] = "-Select a User-";
dsUsers.Tables[0].Rows.InsertAt(dr,0);
And you can set your source as usual
ddlUsers.DataSource = dsUsers.Tables[0];
ddlUsers.ValueMember = "userID";
ddlUsers.DisplayMember = "Username";

Add datagridviewrow with mixed column types using addrange

I have a datagridview that gets populated with data from a database, the last two columns are a combobox and a button.
To prevent flickering I use Rows.AddRange to add all the rows at once (the entire procedure is in a backgroundworker)
My question is how do I add the values to the dropdownbox. The items are just a list of stings so no need to datasource it.
DataTable dt = db.fill(query, dbpars);
DataGridViewComboBoxCell cbox = new DataGridViewComboBoxCell();
cbox.Items.Add("--Please Select--");
cbox.Items.Add("Generate");
cbox.Items.Add("Ignore");
List<DataGridViewRow> rowList = new List<DataGridViewRow>();
foreach (DataRow row in dt.Rows)
{
DataGridViewRow drow = new DataGridViewRow();
drow.CreateCells(dgvClientWork);
drow.Cells[0].Value = row[0];
drow.Cells[1].Value = row[1];
drow.Cells[2].Value = row[2];
drow.Cells[3].Value = row[3];
drow.Cells[4].Value = row[4];
drow.Cells[5].Value = row[5];
//((DataGridViewComboBoxColumn)dgvClientWork.Columns[6]).Items.Add("2");
//var td = new DataGridViewComboBoxCell();
//drow.Cells[6] = td;
//((DataGridViewComboBoxCell) drow.Cells[6]).Items.Add("WFT");
DataGridViewComboBoxCell td = (DataGridViewComboBoxCell)dgvClientWork.Rows[0].Cells[6];
td.Items.Add("--Please Select--");
td.Items.Add("Generate");
td.Items.Add("Ignore");
//td.Items.AddRange(new object[]{"--Please Select--", "Generate", "Ignore"});
//var t = (DataGridViewComboBoxCell)cbox.Clone();
//drow.Cells.Add(t);
//drow.Cells[6] = t;
//drow.Cells[6].Value = "--Please Select--";
/*drow.Cells[7].Value = btn;*/
rowList.Add(drow);
}
Action action = () => dgvClientWork.Rows.AddRange(rowList.ToArray());
dgvClientWork.Invoke(action);
As you can see I have attempted several things but the combobox is always blank.
If I duplicated your problem correctly, if you check the last row in your dgv, that drop down will have multiples of your added items.
To fix, change this line of code:
DataGridViewComboBoxCell td = (DataGridViewComboBoxCell)dgvClientWork.Rows[0].Cells[6];
To this:
DataGridViewComboBoxCell td = (DataGridViewComboBoxCell)drow.Cells[6];
Edit: If you want the cells defaulted, just adding
td.Value = "--Please Select--";
then worked for me.
Edit: On a side note, when you created your DGV and added the columns, I would have just created the ComboBoxColumn and set its DataSource to a list of the same strings you've set in your code. This way any newly added rows by the user will also have the same combobox source. As of right now, any new rows will have empty comboboxes.

Add summary to a datagridview in windows Form Application

I want to add a row in datagridview under my last row which shows summary of the records.So I Fill my dataset and then add a row in it, afterwards I Bind it to a datagridview and then when I try to assign my new row with value it gives me error that cannot convert date time to string as The fourth column datatype is datetime.SO my question is can we change column type of a specific row cell ,If no then how can I achieve what I want to do?
string SelectGroupQuery = "Select * From GroupMembers Where GID=#Id ";
using (SqlConnection conGroup = new SqlConnection(ConnectionString.ToString()))
{
using (SqlCommand commandGroup = new SqlCommand(SelectGroupQuery, conGroup))
{
commandGroup.CommandType = CommandType.Text;
commandGroup.Parameters.Add(new SqlParameter("Id", Id));
SqlDataAdapter da = new SqlDataAdapter(commandGroup);
DataSet ds = new DataSet();
da.Fill(ds);
d1 = new DataGridView();
this.Controls.Add(d1);
d1.Location = new Point(50,y);
d1.Size = new Size(600, 300);
dr = ds.Tables[0].NewRow();
ds.Tables[0].Rows.Add(dr);
d1.DataSource = ds.Tables[0];
d1.Columns[4].ValueType = typeof(string);
d1.Rows[d1.Rows.Count-2].Cells[4].Value = "Total Amount";
y = y + 400;
}
}
Changing the type of a particular cell could be done for some of the column types. See this answer. But I wouldn't recommend it.
It is tricky to use a row in datagridview for displaying summary as it brings in some problems (more when bound to database). You could create your summary row using textboxes outside the datagridview and add the summary data to them. Check this codeproject link for working example.
Instead of assigning the value to the cell, you could fake it by handling the CellFormatting event. If e.ColumnIndex = 4 and e.RowIndex is the last row in the grid, set the formatted value to the label you want and set the property of the EventArgs to tell it you formatted the value.
If you aren't making the whole DataGridView read-only, you probably want to also handle the CellBeginEdit event and cancel the edit if it's the summary row.

Adding rows and columns programmatically [duplicate]

I'm sending values from one form to another form, then want to display in dgv,
I'm trying this, there is no error during execution, bt it does not show data in dgv..
lineItemsDGV.Rows.Add();
int RowIndex = lineItemsDGV.RowCount - 1;
DataGridViewRow NewRow =lineItemsDGV.Rows[RowIndex];
NewRow.Cells[0].Value = item.Product_id;
NewRow.Cells[1].Value = item.product_name;
NewRow.Cells[2].Value = item.specification;
NewRow.Cells[3].Value = item.unit_price;
NewRow.Cells[4].Value = item.unit_price * 1;
You're close.
What you can do is use the return value of your call to DataGridViewRowCollection.Rows.Add() method, which is the index value of the just added row.
Change your code to this:
int RowIndex = lineItemsDGV.Rows.Add();
DataGridViewRow NewRow = lineItemsDGV.Rows[RowIndex];
NewRow.Cells[0].Value = item.Product_id;
NewRow.Cells[1].Value = item.product_name;
NewRow.Cells[2].Value = item.specification;
NewRow.Cells[3].Value = item.unit_price;
NewRow.Cells[4].Value = item.unit_price * 1;
This is how I would do it:
DataRow rowToAdd= myTable.NewRow();
rowToAdd["ProductId"] = item.Product_id;
rowToAdd["ProductName"] = item.product_name;
rowToAdd["Specification"] = item.specification;
rowToAdd["UnitPrice"] = item.unit_price;
myTable.Add(rowToAdd);
And bind the datatable to the gridview.
I know this thread is old, but I found this page helpful today and this is how I accomplished adding a new row which is different than the previous solutions.
Assumption:
datagridview has columns specified at design time. In my case I had 3 text columns.
The solution is to use the DataGridViewRowCollection.Add functionality and pass a parameter array that contains a list of each column value separated by a comma.
DataGridView1.Rows.Add("Value 1", "Value 2", "Value3")
Using this approach, there's no need to use the BeginEdit, Update or Refesh dgv functions.

CheckedComboboxEdit check item does not work properly

I used a CheckedComboBoxEdit control. It is filled by a dataTable. And I checked an item programatically.The following picture shows the output:
It shows that, no items are selected in drop down menu. I did not understand the problem.
Edit: I found the source of the problem. However I do not know, how can I fix this and why it causes the problem.
My code:
rHOP rGetir = new rHOP();
DevExpress.XtraEditors.Repository.RepositoryItemCheckedComboBoxEdit propertiesBaslattigiSurecler = cceBaslattigiSurecler.Properties;
DataTable dt = rGetir.GetirSurecTanim(0, 0);
dt.Columns.Add("Deger", typeof(string));
for (int i = 0; i < dt.Rows.Count; i++)
dt.Rows[i]["Deger"] = dt.Rows[i]["Süreç No"].ToString()
+ "," + dt.Rows[i]["Sürüm"].ToString();
propertiesBaslattigiSurecler.DataSource = dt;
propertiesBaslattigiSurecler.DisplayMember = "Süreç Ad";
propertiesBaslattigiSurecler.ValueMember = "Deger"; // this line causes the problem
cceBaslattigiSurecler.SetEditValue(null);
The dataTable dt has three columns as "Süreç Ad", "Süreç No", "Süreç Ack". Then I added "Deger" column to the dataTable. When I set the ValueMember property to "Deger" column, the problem occurs. When I set the ValueMember property to another column, it works correctly.

Categories

Resources