CheckedComboboxEdit check item does not work properly - c#

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.

Related

DataRow created by DataTable.NewRow() can't be accessed through BindingManager.Position

Sorry for my bad English, hope you can understand my situation.
A table in database will be shown in TextBoxs from a Form with DataBinding, like the following:
DataTable dataTable = dataBase.ReadTable(tableName); //Suppose: dataTable.Rows.Count == 10
BindingManagerBase bindingManager = BindingContext[dataTable];
BindTextBoxsWith(dataTable);
ActionToChange(bindingManager.Position);
Everthing works fine. Then I need to add a new row and switch to the new row for editing, so these code are added:
DataRow dataRow = dataTable.NewRow();
dataRow[primaryKey] = 0;
dataTable.Add(dataRow);
bindingManager.Position = dataTable.Rows.Count - 1; //value in breakpoint: dataTable.Rows.Count == 11, bindingManager.Position == 0
As shown in the comment, the BindingManager's Position don't work in a normal style. How can it be?
Then I try these code:
dataTable = dataTable.Copy();
BindTextBoxsAgainWith(dataTable);
bindingManager = BindingContext[dataTable];
Now bindingManager.Position can work, but the problem is when bindingManager.Position changes, a same row is bound everytime.
What caused this problem? Expect your help.

Datagrid won't clear

The datagrid I have made won't let me clear the values. When I attempt to do a datagrid.columns.clear(); then it scrunches the rows together and doesn't display anything more than lines and it still doesn't clear.
I have tried datagrid.items.clear(); as I have used Datagrid.items.add(); to add items to my datagrid. I have tried datagrid.items.refresh(); and setting the datacontext to null. None of this is working.
public void FillDataGridWithCustInfo()
{
CustomerDataGrid.Items.Clear();
CustomerDataGrid.ItemsSource = null;
CustomerDataGrid.Items.Refresh();
int ArrayNum = 0;
Int32 length = CustomerFirstName.Count;
while (length > ArrayNum)
{
CustomerInformation TempCust = new CustomerInformation();
TempCust.FirstName = CustomerFirstName[ArrayNum];
TempCust.MiddleInital = CustomerMiddleInitial[ArrayNum];
TempCust.LastName = CustomerLastName[ArrayNum];
TempCust.TaxClass = CustomerTaxClass[ArrayNum];
TempCust.Email = CustomerEmail[ArrayNum];
CustomerDataGrid.Items.Add(TempCust);
ArrayNum = ArrayNum + 1;
}
}
I want it to clear the datagrid before filling it when I close out of this particular window, but it is currently taking all the data in the datagrid and just adding to it so I am getting duplicates of all the information.
I am not sure is datagrid was use by desktop application , in web application we have a similar object call as gridview which show data as table format. If you want to force clear all row , you can assign an empty or dummy data table , something like below shall do the trick..
GridView1.DataSource = new DataTable();
GridView1.DataBind();
If you want to maintain proper column header and just want to remove row that contain data , make sure you proper structure your datatable..something like this.
DataTable dt = new DataTable();
dt.Columns.Add("Column1", Type.GetType("System.String"));
dt.Columns.Add("Column2", Type.GetType("System.String"));
GridView1.DataSource = dt;
GridView1.DataBind();

Child list for field " " cannot be created

I have the following set of codes for adding values to my DataGridView through DataTable as my datasource. However, it keeps on giving me the error "Child list for field tbl_main cannot be created". Anybody can help me identify the problem?
dgvMySchedule.Columns.Clear();
dgvMySchedule.Rows.Clear();
dgvMySchedule.ClearSelection();
dataSet.Tables.Add(tbl_main);
dgvMySchedule.DataSource = dataSet;
dgvMySchedule.DataMember = "tbl_main";
tbl_main.Columns.Add("TIME");
tbl_main.Columns.Add("CLASS");
DataRow row;
dgvMySchedule.RowTemplate.Height = 8;
for (int i = 0; i <= 71; i++)
{
row = tbl_main.NewRow();
row["TIME"] = i;
row["CLASS"] = i;
tbl_main.Rows.Add(row);
}
It looks like your tbl_main is unnamed, or it has a different name than "tbl_main". There are three possible solutions:
Remove the DataSet, instead use dgvMySchedule.DataSource = tbl_main;
If you need/want the DataSet, add tbl_main.TableName = "tbl_main";
If you need/want the DataSet, remove the line saying dgvMySchedule.DataMember = "tbl_main" (the grid will then automatically use the one DataTable present).
I finally realized my mistake. I forgot to initialize my datatable during declaration. I used DataTable tbl_main = new DataTable(); instead of DataTable tbl_main = new DataTable("tbl_main"); The changes pretty much answered my concern. Thank you!
I had this problem using binding in winforms.
I had set binding to a non existent field name.

Combobox default value is always SQL table value - 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;

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.

Categories

Resources