Repository item lookUpEdit resets after losing focus - c#

I have this problem for days and can't find solution for it. I tried all possible solutions i found on internet, but seems like none suits this one.
Thing is that i added repository item to gridControls (i added it through designer, not through code). Then, in code i added data source to that repository lookUpEdit and i have items in dropDown in that column. But when i select item in repository and click on other cell, Selected item in repository is cleared and repository shows null value again...
Any ideas what i did wrong ?
EDIT:
Also, when i click on any cell in my grid, i have delay of second or two, and after that delay clicked cell is focused... Any solutions for all of this?
EDIT:
Don't know what code to show You, because I did all in devExpress designer. Here is part of the code where I set data source to repository item, and i will give You code from designer of that repository item.
private void ConfigureRepositoryItems()
{
BetService.SportManagerClient dbSportManager = new BetService.SportManagerClient();
BetService.BLOddsControlSettings[] oddsControlSettings = dbSportManager.GetOddsControlSettings("", "");
repositoryOddsControlSettings1.DataSource = oddsControlSettings;
}
And here is code from designer:
//
// repositoryOddsCalculationSettings1
//
this.repositoryOddsCalculationSettings1.AutoHeight = false;
this.repositoryOddsCalculationSettings1.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
this.repositoryOddsCalculationSettings1.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
new DevExpress.XtraEditors.Controls.LookUpColumnInfo("ID", "ID", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Name", "Name")});
this.repositoryOddsCalculationSettings1.DisplayMember = "Name";
this.repositoryOddsCalculationSettings1.Name = "repositoryOddsCalculationSettings1";
this.repositoryOddsCalculationSettings1.NullText = "Select Settings";
this.repositoryOddsCalculationSettings1.PopupSizeable = false;
this.repositoryOddsCalculationSettings1.ValueMember = "ID";

For starters check whether the column name in your Grid datasource and the column in your grid control match. The match is case sensitive so name and Name are not same and hence can cause this issue. Secondly make sure the Grid datasource column datatype matches the value type of the LookUpEdit. If the LookupEdit is returning int and the Grid datasource column datatype is string, this alone can cause lots of headaches.

Related

How to group items in ObjectListView?

I have specific case of grouping items in ObjectListView. Usually one would choose column that is shown in the list and do GroupKeyGetter/GroupKeyToTitleConverter magic on that column. However, in my case the data I would like to group on is not supposed to be shown in ObjectListView. It exists only in Model. So far the only dirty workaround I've found is to make this data shown in ObjectListView, but set width of column 0.
Is there better way to group by data, which is not supposed to be shown in the view?
You can hide your column:
OLVColumn column = new OLVColumn("HiddenGroupColumn", "ModelProperty");
column.IsVisible = false;
column.GroupKeyGetter = delegate(object x)
{
return ((Model)x).ModelProperty;
};

Set SelectedValue in DataGridViewComboBoxColumn or DataGridViewComboBoxCell

I would like to set a value to the ComboBox in the DataGridView. I already have changed the comboBoxItems, I just want to select one of them. Thank you in advance!!!
I already solved my problem... I'm gonna post the way I did and hoppefully someone will find this answer too.
dgrDetalle.DataSource = dataTable("select * from yourTable");
DataTable dtCombo = dataTableCombo("select COL_ID DETOC_COL_FK,COL_DESCRIPCION from yourTable2");
string[] strColumns = new string[] { "COL_DESCRIPCION" };
MultiColumnDictionary map = new MultiColumnDictionary(dtCombo, "DETOC_COL_FK", strColumns, 0);
dgrDetalle.Cols["DETOC_COL_FK"].DataMap = map;
As you can see the class that save my life is MultiColumnDictionary.
Note: The combobox items must be loaded in a different DatatTable than the DataTable that is gonna load directly in the grid.
As far as I know, the Comboboxes only actually exist as controls when they are being edited, and therefore don't have a selected item property.
You can simply set the Value property of the cell to the item you want selected, or alternitively, you can set a default value by setting the property:
DataGridViewColumn.DefaultCellStyle.NullValue.

datatable.GetChanges() shows always first row of DataGridView

I have a DataGridView with a Binding-Source bound to a Data-Table loaded from an Oracle-Database. (btw. I don't think that the Database-Connection could cause this)
I have also a DataGridViewComboBoxColumn bound to an class of persons (only "has" ID and name), so I can display / allow to edit the name instead of the ID. My problem is now that after the data-binding is completed c# automatically "selects" the first cell of the DGV - see the attached picture.
I also have to use this little piece of code to ensure data integrity:
private void _table_ColumnChanging(object sender, DataColumnChangeEventArgs e)
{
if (e.Column == _table.Columns["NEEDED_ID"])
{
if (e.ProposedValue == null)
{
e.ProposedValue = System.DBNull.Value;
}
}
}
Now using this _table.GetChanges() always returns the first row of the DGV as "modified" but the value isn't really changed - it's only DBNull instead of null. Can I somehow avoid automatic selection of first cell or how to avoid this behavior?
EDIT: Meanwhile I found out that changing the first column to something not editable fixes this problem. But this is nothing more than an workaround. I would really appreciate to get an working solution for this.
EDIT 2: I have also an empty Object 'on top' of the ComboBox-DataSource
DtoPerson blankPerson = new DtoPerson();
blankPerson.Name = String.Empty;
blankPerson.PersonRollenId = -1;
personList.Add(blankPerson);
Try to put a null in your DataGridViewComboBoxColumn's DataSource. example:
SELECT id, description
FROM yourTable
UNION
SELECT NULL, NULL
GridViewName.ClearSelection();

C# datagridview setting cell value stays null

I'm dynamically adding rows to a datagridview this way:
Question question = new Question();
List<Question> questions = question.GetQuestionsByQuestionnaire(questionnaireId);
if (questions != null)
{
dgvQuestions.Columns.Add("Question", "Vraag");
dgvQuestions.Columns.Add("QuestionType", "Vraag type");
dgvQuestions.Columns.Add("Category", "Categorie");
for (int i = 0; i < questions.Count; i++ )
{
int row = dgvQuestions.Rows.Add();
dgvQuestions.Rows[row].Cells["Question"].Value = questions[i].Question;
dgvQuestions.Rows[row].Cells["QuestionType"].Value = questions[i].QuestionType;
dgvQuestions.Rows[row].Cells["Category"].Value = questions[i].Category;
dgvQuestions.Rows[row].Tag = questions[i];
}
}
I don't get any errors, but the cell value stays null and I'm 100% sure that Question, QuestionType and Category contains data. What am i missing here?
I'm not sure about why this is the case, but I'd go for a mix of dynamic data but typed dataset.
What you'd do is:
Create a typed DataSet, add a "Questions" table with the columns you need
Put an instance of your DataSet from the Toolbox on your form (must recompile before that), name it for example myDataSource.
Put a BindingSource on your form, assign the myDataSource to the DataSource property and select your table for the DataMember property.
Assign the binding source to the DataSource property of your DataGridView
Add data to the data source by using for example myDataSource.Questions.NewQuestionsRow() and myDataSource.Questions.AddQuestionsRow(...).
I've just encountered something similar. You might want to make sure EnableViewState is set to True for your GridView.
Make sure VitualMode is set to False for your GridView.

DataBinding a DataGridview rows BackColor

I have a DataGridView that uses databinding, with manually created columns, and this works fine.
However I want the rows' BackColor to be databound as well, and so far my attempts have hit errors.
This is my latest attempt:
dataGridFileTransfer.RowHeadersVisible = false;
dataGridFileTransfer.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridFileTransfer.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridFileTransfer.MultiSelect = false;
dataGridFileTransfer.ReadOnly = true;
var files = GetReceivedFiles(false).Union(FileDetails.Select(FileStatus.FailedVerification)).ToList();
dataGridFileTransfer.AutoGenerateColumns = false;
string[] displayHeaders = new string[] { COL_NAME, COL_TYPE, COL_CREATED, COL_SIZE, COL_STATUS };
string[] displayProps = new string[] { "Filename", "FileTypeDisplayName", "Created", "Size", "FileStatus" };
for (int i = 0; i < displayHeaders.Length; i++)
{
DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
col.HeaderText = displayHeaders[i];
col.DataPropertyName = displayProps[i];
if (displayHeaders[i] == COL_CREATED)
col.DefaultCellStyle.Format = Constants.DDMMYYYHHMMSS;
dataGridFileTransfer.Columns.Add(col);
}
Binding bi = new Binding("DefaultCellStyle.BackColor", files, "DisplayColor");
dataGridFileTransfer.DataBindings.Add(bi);
dataGridFileTransfer.DataSource = files;
Which is generating an ArguementException:
"Cannot bind to the property
"DefaultCellStyle.BackColor' on the
target control. Parameter name:
PropertyName"
Is it the value of PropertyName that is wrong, or should I binding to an object other than the DataGridView? (i.e. a column?)
Or is the problem that PropertyName cannot be in the form X.Y? I thought I had seen/used this syntax before, but maybe it only works for DataMember?
Any help is much appreciated
I think the problem is files.DisplayColor. files is a collection an has no property DisplayColor but each item of the collection has. So you are trying to bind a not existing property. Further binding collection DataGridView.DataBindings allows you to data bind properties of the control, not of its rows. There is only one DataGridView.DefaultCellStyle.BackColor for all rows. So I believe you end up needing to bind the DefaultCellStyle of each row to the coresponding item from files and I am not sure if this is possible. It might be that the DataGridView creates and deletes rows as required - for example if you perform filtering - and this will destroy the data binding, too.
So, I am not sure if row coloring can be done with data binding, but I personaly doubt it. This would require some really smart logic recognicing 'bind the property DisplayColor of the object data bound to this row to the property DefaultCellStyle.BackColor of this row.'
You could surly implement such an smart data binding. While it would be a great thing, it will be quite complex, too. As a simple solution you could just use the RowPrepaint event to set the correct color for the row.

Categories

Resources