i know that there are many examples how to show SelectItem / ComboBox records in grid, so I serched over two days for solution but so far I havnt found out where is my mistake. Well what I triyng do is to add some data in ListGrid which will appear when in SelectItem. But the problem is That my grid doesnt appear. Let me show you a bit source :
final SelectItem item = new SelectItem("id1", "Item1");
final ListGrid grid = new ListGrid();
grid.setShowAllColumns(true);
grid.setShowAllRecords(true);
grid.setShowEmptyMessage(true);
grid.setShowRowNumbers(true);
final ListGridField fieldOne = new ListGridField("number", "Number");
final ListGridField fieldTwo = new ListGridField("time", "Time");
grid.setFields(new ListGridField[]{fieldOne, fieldTwo});
AsyncService service = getAsyncService();
service.getData(new AsyncCallback<List<String[]>>() {
#Override
public void onFailure(Throwable caught) {
SC.warn("ERROR");
}
#Override
public void onSuccess(List<String[]> result) {
final ListGridRecord[] records = new ListGridRecord[result.size()];
for (int i = 0; i < result.size(); i++) {
final ListGridRecord record = new ListGridRecord();
record.setAttribute("number", result.get(i)[1]);
record.setAttribute("time", result.get(i)[2]);
records[i] = record;
}
grid.setRecords(records);
}
});
item.setPickListProperties(grid);
So whats аm I wrong ?
Thank you
Related
I am not very experienced with Windows Forms and am not pretty sure how I should tackle with this task the best way possible. I have a class which looks like this:
public class VariableMapping
{
private string variableName;
private string variableText;
private string variableSelector;
public VariableMapping(string variableName, string variableText, string variableSelector)
{
this.VariableName = variableName;
this.VariableText = variableText;
this.VariableSelector = variableSelector;
}
public string VariableName
{
get { return this.variableName; }
set { this.variableName = value; }
}
public string VariableText
{
get { return this.variableText; }
set { this.variableText = value; }
}
public string VariableSelector
{
get { return this.variableSelector; }
set { this.variableSelector = value; }
}
}
I want to create a DataGridView which should be bound to a number of elements of type VariableMapping in a list. However, I want only 1 of the properties(VariableText) of every instance to be shown in the DataGridView but I want to be able to address the whole object through the DataGrid when I need to. I also need to add 2 more custom columns: a ComboBox with predefined values and a NumberBox.
It might seem a really simple task but I'm trully unexperienced in WinForms and couldn't find a solution I can use already. Thank you!
Edit: I am trying something like this but it doesn't seem to work properly:
public partial class MappingTable : Form
{
private DataGridView dataGridView1 = new DataGridView();
public MappingTable(List<VariableMapping> variableMappings)
{
InitializeComponent();
var colors = new List<string>() { "#color_k1", "#color_k2", "#color_s1" };
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSize = true;
dataGridView1.DataSource = variableMappings;
DataGridViewColumn titleColumn = new DataGridViewColumn();
titleColumn.DataPropertyName = "VariableText";
titleColumn.HeaderText = "Variable";
titleColumn.Name = "Variable*";
dataGridView1.Columns.Add(titleColumn);
DataGridViewComboBoxColumn colorsColumn = new DataGridViewComboBoxColumn();
colorsColumn.DataSource = colors;
colorsColumn.HeaderText = "Color";
dataGridView1.Columns.Add(colorsColumn);
DataGridViewTextBoxColumn opacityColumn = new DataGridViewTextBoxColumn();
opacityColumn.HeaderText = "Opacity";
dataGridView1.Columns.Add(opacityColumn);
this.Controls.Add(dataGridView1);
this.AutoSize = true;
}
}
I'm new to Java and coding all together so I have hit a wall with my code. I've read nearly every post on this subject and still can't grasp it. Can somebody help?
Here is my code and permissions are already set in the manifest. Basically when I try to delete an item/file from a listview using onLongClick a file deletes, just not the one I want. The item first on the list deletes every attempt. I'm not sure how to solve this. I know that the code is half right considering the file does delete from the directory on the SD and from the listview. Getting the right file to delete is the problem. Any help would be appreciated.
public class ReadNoteMenu extends ActionBarActivity {
public static final String EXTRA_MESSAGE = "com.m4tchb0x87.rhinote.MESSAGE";
Context context;
public ReadNoteMenu() {
this.context = this;
}
ArrayAdapter mArrayAdapter;
ListView listView;
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
this.setContentView(R.layout.activity_read_note_menu);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
listView = (ListView) this.findViewById(R.id.readListView);
String fileNames[] = new File(String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath()) + "/Rhinote").list();
mArrayAdapter = new ArrayAdapter<>(this, R.layout.list_item_1, fileNames);
listView.setAdapter(mArrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view, int i, long l) {
String string = (String) listView.getItemAtPosition(i);
Intent intent = new Intent(ReadNoteMenu.this, (Class) ReadNote.class);
intent.putExtra(EXTRA_MESSAGE, string);
ReadNoteMenu.this.startActivity(intent);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView adapterView, final View view, int position, long l) {
ContextThemeWrapper CTW = new ContextThemeWrapper( context, R.style.ADM_theme);
MaterialDialogCompat.Builder MDM1 = new MaterialDialogCompat.Builder(CTW);
MDM1.setMessage("Edit or delete file?");
MDM1.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
ContextThemeWrapper CTW = new ContextThemeWrapper( context, R.style.ADM_theme);
MaterialDialogCompat.Builder MDM2 = new MaterialDialogCompat.Builder(CTW);
MDM2.setMessage("Confirm delete file?");
MDM2.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
String str = (String) listView.getItemAtPosition(position);
Log.d(str,"Deleted" );
new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/RhiNote" + "/" + str).delete();
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_deleted,
(ViewGroup) findViewById(R.id.toast_layout_root_deleted));
TextView text = (TextView) layout.findViewById(R.id.text_deleted);
text.setText("Note deleted!");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP, 250, 25);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
ReadNoteMenu.this.finish();
ReadNoteMenu.this.startActivity(ReadNoteMenu.this.getIntent());
}
});
MDM2.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int which) {
ReadNoteMenu.this.startActivity(ReadNoteMenu.this.getIntent());
}
});
MDM2.show();
}
});
MDM1.setNegativeButton("Edit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int which) {
ReadNoteMenu.this.startActivity(ReadNoteMenu.this.getIntent());
}
});
MDM1.show();
return true;
}
});
}
}
I figured out where I went wrong after a little debugging. It seems that the problem was that I hadn't declared the onItemLongClick int position final.
public boolean onItemLongClick(AdapterView adapterView, final View view, final int position, long l)
Problem:
I have a Winform application with a form en on this form i have a databound DataGridView.
The datagridview is updated from the backend by updating the bind object continuesly using a timer to get the data every 10 seconds. In order to update the gui with this new data i call a RefreshDatabindings. (if i do not do this, the gui is nog updated, i am binding to a BindingList and the object implement the INotifyPropertyChanged)
When the form is big enough to show the whole datagridview at once everything is working wel. But when the form is not big enough to show the hole datagridview a scrollbar appears.
When i scroll to the right to see the rest of the datagridview i see the gui flickering (only the part that wasn't visible before scrolling). When i strech the form to make de gridview fitting again, everything is working wel (no flashing and flickering). the flickering only happens when i have to scroll.
I am lost, can please somebody help me :)?
I allready tryed the DoubleBuffered = true.
Thanks in advance!
BindingList<InstanceTableViewModel> viewModelList;
public Form1()
{
InitializeComponent();
DoubleBuffered = true;
functionParamList = new List<FunctionParameter>();
functionParamList.Add(new FunctionParameter { DeviceValue = 100, InstanceId = "1", Name = "A" });
functionParamList.Add(new FunctionParameter { DeviceValue = 200, InstanceId = "2", Name = "B" });
functionParamList.Add(new FunctionParameter { DeviceValue = 300, InstanceId = "3", Name = "C" });
viewModelList = CreateInstanceTableViewModelList();
dataGridView1.DataSource = viewModelList;
//Create timer
updateDataTimer = new System.Timers.Timer();
updateDataTimer.Interval = 500;
updateDataTimer.Elapsed += updateDataTimer_Elapsed;
updateDataTimer.Start();
}
private void updateDataTimer_Elapsed(object sender, ElapsedEventArgs e)
{
ThreadPool.QueueUserWorkItem(ReadDataThreadPoolMethod);
}
private void ReadDataThreadPoolMethod(object state)
{
Random random = new Random();
int randomNumber = random.Next(0, 100);
foreach (FunctionParameter param in functionParamList)
{
param.DeviceValue = Convert.ToInt64(randomNumber);
}
}
void functionParameter_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var functionParameter = sender as FunctionParameter;
var propertyName = e.PropertyName;
var propertyValue = functionParameter.DeviceValue;
var parameterName = functionParameter.Name;
UpdateViewModel(functionParameter.InstanceId, propertyName, propertyValue, parameterName);
}
private void UpdateViewModel(string instanceId, string propertyName, long propertyValue, string parameterName)
{
var instanceViewModel = viewModelList.Single(x => x.InstanceId == instanceId && x.NameLabel == parameterName);
if (instanceViewModel != null)
{
instanceViewModel.ValueHex = Convert.ToUInt16(propertyValue);
}
ResetBindingsSource();
}
delegate void UpdateBindingsInvoker();
public void ResetBindingsSource()
{
if (!this.IsDisposed)
{
if (this.InvokeRequired)
{
this.Invoke(new UpdateBindingsInvoker(UpdateDataGrid));
}
else
{
UpdateDataGrid();
}
}
}
private void UpdateDataGrid()
{
dataGridView1.Refresh();
}
So here my solution:
You only uses the Forms DoubleBuffering, but the following code is an extension method to the DataGridview and successfully works (at my tests ;)
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}
I found this code right here at Codeprojct.
You can use it in this way:
YourDataGridView.DoubleBuffered(true);
I hope i could help you ^^
Never used the datagridview before and I cannot figure out how to change myImageColumn depending on status.
I am loading some logs in this grid and again depending on the status I would like to assign the appriopriate image.Not sure which event I should be doing it.
Any suggestions or example with bound or unbound would be great.
Here is some code
public enum LogType
{
Fatal,
Error,
Warn,
Info,
Debug,
None,
}
public class Log
{
public LogType LogType { get; internal set; }
public string Message { get; set; }
}
private void LoadDataGrid()
{
// Create the image column.
DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
imageCol.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
imageCol.ImageLayout = DataGridViewImageCellLayout.Normal;
imageCol.Frozen = true;
imageCol.Name = "Image";
imageCol.HeaderText = "";
imageCol.DisplayIndex = 0;
imageCol.Image = Properties.Resources.warning;
datagrid.Columns.Add(imageCol);
DataGridViewTextBoxColumn colMessage = new DataGridViewTextBoxColumn();
colMessage.Name = "Message";
colMessage.HeaderText = "Message";
datagrid.Columns.Add(colMessage);
datagrid.DataSource= GetAllLogs();
}
private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
//I am not sure about the all thing here
// Check if it's the Image column.
if ((dgvLogs.Columns[e.ColumnIndex].Name == "Image"))
{
object value = dgvLogs.Rows[e.RowIndex].Cells["LogType"].Value;
//TODO:Convert to enum
// switch (type)
//{
// case "fatal": e.Value=FatalImage;
// case "error": e.Value=ErrorImage;
//case "warn": e.Value=WarnImage;
//case "info": e.Value=InfoImage;
//case "debug": e.Value=DebugImage;
//}
}
Need help with some code here or link where you can see how images are determined at runtime.
thanks a lot
I found a link which maybe helpful for you.
http://www.informit.com/articles/article.aspx?p=446453&seqNum=14
Hth.
I'm trying to bind a List<T> to a DataGridView control, and I'm not having any luck creating custom bindings.
I have tried:
gvProgramCode.DataBindings.Add(new Binding("Opcode",code,"Opcode"));
It throws an exception, saying that nothing was found by that property name.
The name of the column in question is "Opcode". The name of the property in the List<T> is Opcode.
ANSWER EDIT: the problem was that I did not have the bindable fields in my class as properties, just public fields...Apparently it doesn't reflect on fields, just properties.
Is the property on the grid you are binding to Opcode as well?.. if you want to bind directly to List you would just DataSource = list. The databindings allows custom binding. are you trying to do something other than the datasource?
You are getting a bunch of empty rows? do the auto generated columns have names? Have you verified data is in the object (not just string.empty) ?
class MyObject
{
public string Something { get; set; }
public string Text { get; set; }
public string Other { get; set; }
}
public Form1()
{
InitializeComponent();
List<MyObject> myList = new List<MyObject>();
for (int i = 0; i < 200; i++)
{
string num = i.ToString();
myList.Add(new MyObject { Something = "Something " + num , Text = "Some Row " + num , Other = "Other " + num });
}
dataGridView1.DataSource = myList;
}
this should work fine...
I can't really tell what you're trying to do with the example you included, but binding to a generic list of objects is fairly straightforward if you just want to list the objects:
private BindingSource _gridSource;
private BindingSource GridSource
{
get
{
if (_gridSource == null)
_gridSource = new BindingSource();
return _gridSource;
}
}
private void Form1_Load(object sender, EventArgs e)
{
List<FluffyBunny> list = new List<FluffyBunny>();
list.Add(new FluffyBunny { Color = "White", EarType = "Long", Name = "Stan" });
list.Add(new FluffyBunny { Color = "Brown", EarType = "Medium", Name = "Mike" });
list.Add(new FluffyBunny { Color = "Mottled", EarType = "Short", Name = "Torvald" });
GridSource.DataSource = list;
dataGridView1.Columns["EarType"].Visible = false; //Optionally hide a column
dataGridView1.DataSource = GridSource;
}
If you only want to display specific properties of the List's type you should be able to make the unwanted column(s) invisible.
Technically, you don't really need to create the BindingSource, but I find it's a whole lot easier when I'm doing updates or changes if I have it.
Hope this helps.
Had the same issue... I had a struct with public fields obviously. nothing in the grid. provided public getters, worked.
Another solution I've found is to use the BindingList collection.
private void Form1_Load(object sender, EventArgs e)
{
BindingList people= new BindingList {
new Person {Name="John",Age=23},
new Person {Name="Lucy",Age=16}
};
dataGridView1.DataSource= people;
}
It works fine for me,