Concatenate control name in C# - c#

I don't know how to say this, I have 3 datagridview control (named datagridview1, datagridview2 & datagridview3). My question is can I concatenate the name of those control?
I mean is there a way that I can call it like this: datagridview(n) or datagridview + n
I'm using winforms.
Thanks in advance!

I think what you want is to find the control with a dynamic name?
Something like this perhaps:
Control match = ParentControl.Controls["datagridview" + n];
So if for example all your data grid are in a single panel called "MyPanel", then you can do this:
DataGridView match = MyPanel.Controls["datagridview" + n] as DataGridView;
If however, your data grids do not all belong to the same parent control, then you can find them using the Controls.Find method:
DataGridView match = this.Controls.Find("datagridview" + n, true)[0] as DataGridView;
NOTE: The Controls.Find method returns an array, so you need to select the first element (assuming your control name is unique), it may also be worth checking if the array has any values before trying to access the first element too.
If you want to wrap that in a function, you can do this:
public DataGridView GetDataGridViewForTabNumber(int n){
Control[] matches = this.Controls.Find("datagridview" + n, true);
if(matches.length == 0)
return null;
return matches[0] as DataGridView;
}
and call it like so:
DataGridView dgv = GetDataGridViewForTabNumber(1);//gets datagridview1
NOTE: This is a perfectly valid method if you are creating DataGridView controls dynamically at run time. If however, you are creating them in the designer then, like Tim said, you should give them a more meaningful name and reference them directly.

Try This
DataGridView dg = (DataGridView)this.Controls.Find("datagridview"+n.ToString(), true);

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;
};

Grid column contains int64 values but filter shows strings and doesn't work/

We've got problem with filtering for some of our columns in devexpress gridcontrol. We add the column dynamically (bound-type column) to the grid. The values from the source objects are long type. In the cells it seems that values are fine (since they're aligned to the right without any custom formating on our side) however in filter popup values behave like strings.
For example data set like 1,2,5,11,22,37 the filter list is sorted like 1,11,2,22,5,37 (just like strings) and when we choose one of the available values the filtering does not work (i mean, grid becames empty). Even filters like "Non empty cells" does not work, but when we choose "empty cells" only few from few thousand rows are shown even if most of the cells have no values.
It is important to point out that only dynamically added columns behave that way, the few columns we create every time our module runs work as intended.
The data source is a container (List like).
We're using DevExpress 13.2.
Example of creating 'custom column'
void CreateColumn(GridColumn gridColumn, string fieldName = null, string caption = null, bool visible = true,
bool readOnly = true, UnboundColumnType unboundType = UnboundColumnType.Bound,
int columnWidth = int.MinValue, int minColumnWidth = int.MinValue)
{
gridColumn.Caption = caption;
if (fieldName != null)
gridColumn.FieldName = fieldName;
gridColumn.Visible = visible;
gridColumn.OptionsColumn.ReadOnly = readOnly;
gridColumn.OptionsColumn.AllowEdit = !readOnly;
gridColumn.UnboundType = unboundType;
gridColumn.OptionsFilter.AllowAutoFilter = true;
gridColumn.FilterMode = ColumnFilterMode.Value;
gridColumn.OptionsFilter.AutoFilterCondition = DevExpress.XtraGrid.Columns.AutoFilterCondition.Contains;
if (columnWidth != int.MinValue)
{
gridColumn.Width = columnWidth;
gridColumn.OptionsColumn.FixedWidth = true;
}
if (minColumnWidth != int.MinValue)
gridColumn.MinWidth = minColumnWidth;
}
GridColumn gridColumn = new GridColumn();
CreateColumn(gridColumn, "someName", "someCaption", true, true);
View.Columns.Add(newGridColumn);
That's how it goes in our code (striped most of not related code just to give example process).
#edit
There's invalid cast exception when we add filter like this:
ColumnFilterInfo filter = GetFilter(); //cant really post code of this
ourGrid.MainView.ActiveFilter.Add(column, filter); // VS points here
Unfortunately it doesnt say what and where (except some 'lambda expression') exception is being thrown.
Of course column is the column mentioned above.
#edit
I've found new "tip". The FilterItem objects contain strings for sure, however they should contain long values. How can we influence the creation of these or atleast where to check why those are created like that (we dont do it manually)?
#Edit 19.11.2015
Alright, I had some breakthrough. The columns ('custom') thanks to our mechanism guess their type just fine. Then only problem is that infact our values which custom columns use are stored in Dictionary<string,object>-like collection and we think that thanks to PropertyDescriptor type for columns is fine, but for some reason FilterItem objects have Value of string. We belive that's because DX filter mechanism can't really guess type of "object" so it uses simple ToString on it so FilterItem.Value is string type instead column's data type.
How to overcome this?
We've found the solution and the mistake was on our side. Column creation etc is fine. Somewhere deep, someone has changed value types.

Repository item lookUpEdit resets after losing focus

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.

DataGridView get current selected object

I need to get the currently selected object from da databound DataGridView.
I do not need the object of the current selected cell, but the object on which the whole row is based, in this case a BusinessObject whos properties make the columns of the grid.
I could go over the DataSource, but that itself is just an object and can be a BindingSource or a IBindingList or something like that - so not easy standartized way to get the wanted object.
Behind that is the need to just check the businessObject for a property called IsChanged and ask the user to save or discard the changes, before the bindingsource selects the next item. Therefore I must find out the current object inside RowValidating-Event of the DataGridView, since the BindingSource does not offer an event to stop changing before change occurs.See here for the well known problem
Thanks for reading ;-)
DataGridViewRow.DataBoundItem contains the 'business' object it is bound to.
Here is my code put this into your Person class
public static explicit operator Person(DataRow dr)
{
Person p = new Person();
p.adi = dr.ItemArray[0].ToString();
p.id = Int32.Parse(dr.ItemArray[1].ToString());
p.soyadi = dr.ItemArray[2].ToString();
p.kartNo = dr.ItemArray[3].ToString();
p.dogumTarihi = DateTime.Parse( dr.ItemArray[4].ToString() );
p.adres = dr.ItemArray[5].ToString();
p.meslek = dr.ItemArray[6].ToString();
p.telefon = dr.ItemArray[7].ToString();
p.gsm = dr.ItemArray[8].ToString();
p.eposta = dr.ItemArray[9].ToString();
return p;
}
and this is a update button click
DataRow row = (dataGridView1.SelectedRows[0].DataBoundItem as DataRowView).Row;
Person selected = (Person)row;
You can also use this short code.
Person selected = dataGridView1.SelectedRows[0].DataBoundItem as Person;
What about this way?
foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
{
MessageBox.Show(item.Cells[0].Value.ToString());
}
We can get multiple selected rows data.
Since you did state the IBindingList - yes as others have said the DataBoundItem property will give you what you need - there is an issue with it that I had experienced previously and ended up with a null reference but right now I can not think of the scenario in which it happened.
If you are databound using a BindingSource - you can capture the CurrentChanged, CurrentItemChanged events of your BindingSource , then you need not have an additional IsChanged Property on your B.O. .. , also the underlying datasource could also indicate modified - for example if you have BindingSource bound to a datatable the row would give you a modified flag.
You can get the selected cell value like this
yourDGV.CurrentCell.Value;
If you want the value in the form of a String just use ToString() method like this
yourDGV.CurrentCell.Value.ToString();
This should do it

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.

Categories

Resources