Hi I'm having a bit trouble with locking my columns in my datagrid in silverlight.
void proxy_ListAllGroupsCompleted(object sender, gkws.ListAllGroupsCompletedEventArgs e)
{
grouplist = e.Result;
List<allGroups> source = new List<allGroups>();
for (int i = 0; i < grouplist[0].Count; i++)
{
source.Add(new allGroups()
{
ID = Convert.ToInt32(grouplist[0][i]),
Name = grouplist[1][i],
CreationDate = grouplist[2][i],
Creator = grouplist[3][i]
});
}
mainGroupDG.ItemsSource = source;
mainGroupDG.Columns[0].IsReadOnly = true;
mainGroupDG.Columns[2].IsReadOnly = true;
mainGroupDG.Columns[3].IsReadOnly = true;
}
When I debug I get the error "Index was out of range". Although my datagrid auto-generates the column before I try to lock them.
Thanks for the help.
Wardh
The problem is that when you're setting the IsReadOnly, the columns have yet to be created.
What you need to do is to catch an event from the DataGrid that happens AFTER the columns have been created. for example, you could do this:
private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column.IsReadOnly = true;
}
Related
I have created a windows form application in which there is a ListBox to display items. When I click on an item it gets selected as I have implemented lst_items_SelectedIndexChanged() method, and values are loaded in the controls to be update. But when I change the value from the controls to update the selected index in also called and throws Index Out of Bounds -1 Exception.
Here is my SelectedIndexChanged Code:
private void lst_items_SelectedIndexChanged(object sender, EventArgs e)
{
ShoppingItem myItem = new ShoppingItem();
if (lst_items.SelectedIndex > -1)
{
myItem = itemManager_obj.GetItem(lst_items.SelectedIndex);
txt_amount.Text = myItem.amount.ToString();
txt_description.Text = myItem.description;
cmb_units.SelectedIndex = (int)myItem.unit;
}
}
Here is my Update(change) button code:
private void btn_change_Click(object sender, EventArgs e)
{
ShoppingItem itemToChange = new ShoppingItem();
itemToChange = itemManager_obj.GetItem(lst_items.SelectedIndex);
bool success = false;
itemToChange = ReadIput(out success);
if (success)
{
success = itemManager_obj.ChangeItem(itemToChange,lst_items.SelectedIndex);
lst_items.Items.RemoveAt(lst_items.SelectedIndex);
lst_items.Items.Insert(lst_items.SelectedIndex, itemManager_obj.ToString());
UpdateGUI();
}
}
I am not sure why SelectedIndexChanged is called on update after this line of code is executed:
lst_items.Items.RemoveAt(lst_items.SelectedIndex);
Any idea how can I update without getting an exception index out of bound?
Regards
Store this in an int
lst_items.Items.RemoveAt(lst_items.SelectedIndex);
then do
lst_items.Items.Insert(your int, itemManager_obj.ToString());
You are getting this error because after you remove the selectedIndex, there is no longer an item selected because that item doesn't exist.
if (success)
{
int indexer=lst_items.SelectedIndex;
success = itemManager_obj.ChangeItem(itemToChange,lst_items.SelectedIndex);
lst_items.Items.RemoveAt(indexer);
lst_items.Items.Insert(indexer, itemManager_obj.ToString());
UpdateGUI();
}
I have a datagridview with its ReadOnly set true to prevent people editing.
then I have a button on each row. when I click on a specific button, I wrote:
private void DGGrade_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > 0 && e.ColumnIndex == DGGrade.Columns["Edit"].Index)
{
DGGrade.Rows[DGGrade.CurrentCell.RowIndex].ReadOnly = false;
DGGrade.Rows[DGGrade.CurrentCell.RowIndex].DefaultCellStyle.BackColor = Color.White;
}
But it is not working. please help
I do not know the reason why it is not working but as far as i can tell from my test runs it has to deal how the data is bound. If you use dataGridView1.DataSource = GetDataSource(); then it did not work in my tests. I have read once about some of the drawbacks of automated binding but i could not find it. Here is the code that works. A row is only in EditMode after the User has clicked the button Edit in the corresponding row. I will be back later - let me know if you need more pointers.
public partial class Form1 : Form
{
int rowIndexOfEditableRow = -1;
public Form1() {
InitializeComponent();
CreateDataGridView(dataGridView1);
SetExistingDataGridViewRowsReadOnly();
this.dataGridView1.Columns.Add(GetBtnColumn());
}
private void SetExistingDataGridViewRowsReadOnly() {
DataGridViewRowCollection rows = this.dataGridView1.Rows;
foreach (DataGridViewRow row in rows) {
row.ReadOnly = true;
}
}
It seems that the grid must be filled manually - at least this way the change of ReadOnly works
public void CreateDataGridView(DataGridView dgv)
{
dgv.ColumnCount = 3;
dgv.Columns[0].Name = "Id";
dgv.Columns[1].Name = "Lastname";
dgv.Columns[2].Name = "City";
dgv.BackgroundColor = Color.Gray;
AddRowsToDataGridView(dgv);
}
private void AddRowsToDataGridView(DataGridView dgv)
{
string[] row1 = new string[]{"1", "Muller", "Seattle"};
string[] row2 = new string[]{"2", "Arkan", "Austin"};
string[] row3 = new string[]{"3", "Cooper", "New York"};
object[] rows = new object[] { row1, row2, row3 };
foreach (string[] rowArray in rows)
{
dgv.Rows.Add(rowArray);
}
}
Helper method to create a column with a button
public DataGridViewButtonColumn GetBtnColumn()
{
DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
btnColumn.HeaderText = "Edit";
btnColumn.Text = "Edit";
btnColumn.UseColumnTextForButtonValue = true;
return btnColumn;
}
Event handler checks if the user has clicked the edit button. In this case the current row will be set to ReadOnly = false. This allows that the user to edit the row. To emphasize it i changed the background color of the row.
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int colIndex = e.ColumnIndex;
int rowIndex = e.RowIndex;
Type cellType = dataGridView1.Columns[colIndex].CellType;
if (cellType == typeof(DataGridViewButtonCell))
{
dataGridView1.Rows[rowIndex].ReadOnly = false;
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = Color.GreenYellow;
this.rowIndexOfEditableRow = rowIndex;
label1.Text = rowIndexOfEditableRow.ToString() + " " + colIndex.ToString();
}
}
If the Row-leave-Event is fired the style is reset and the global parameter which row is editable is set to the initial value
private void DataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
dataGridView1.Rows[rowIndex].ReadOnly = true;
dataGridView1.Rows[rowIndex].DefaultCellStyle.BackColor = Color.White;
this.rowIndexOfEditableRow = -1;
}
}
The above code contains all (except the designer files) that you need to create this demo:
My question was how to get the item or value of a selecteditem out of my datagrid that gets filled by a obc of my view. From te result that i get it seems i need to cast it but all the casts i found from google and here are not working.
Anyone got some tips or solution?
public partial class req: Page
{
DataClasses1DataContext dc = new DataClasses1DataContext();
public requests()
{
InitializeComponent();
//get from Database View
var query = from r in dc.requestViews select r;
this.gridRequest.ItemsSource = new ObservableCollection<requestView>(query);
this.gridRequest.ColumnWidth = 122.7;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
if (gridRequest.SelectedItem == null)
{
MessageBox.Show("Select a Order!");
}
else
{
var qres = (from r in dc.orders where r.id.Equals(gridRequest.SelectedValue) select r).FirstOrDefault();
qres.order_status_id = 3;
dc.SubmitChanges();
MessageBox.Show("Request Accepted");
this.NavigationService.Refresh();
}
}
I try to add a RepositoryItemCheckEdit to my GridView using devexpress and Winforms. However, I can get only one checkbox be checked. If I check another one, the checkbox I checked before becomes unchecked. I followed everything I can find on the net, but couldn't make this work. What am I missing?
The code part I insert the column:
gcIsEmirleri.DataSource = (from i in isemirleri
select new
{
ID = i.isEmriId,
// other attributes
}).ToList();
GridColumn column = gvIsEmirleri.Columns["Sec"];
if (column == null)
{
gvIsEmirleri.BeginUpdate();
DataColumn col = new DataColumn("Sec", typeof(bool));
column = gvIsEmirleri.Columns.AddVisible("Sec");
col.VisibleIndex = 0;
col.Caption = "Sec";
col.Name = "Sec";
col.OptionsColumn.AllowEdit = true;
gvIsEmirleri.EndUpdate();
gvIsEmirleri.Columns["Sec"].UnboundType = DevExpress.Data.UnboundColumnType.Boolean;
RepositoryItemCheckEdit chk = new RepositoryItemCheckEdit();
chk.ValueChecked = true;
chk.ValueUnchecked = false;
gvIsEmirleri.Columns["Sec"].ColumnEdit = chk;
chk.QueryCheckStateByValue += chk_QueryCheckStateByValue;
}
The code part I make the checkbox two-stated instead of three:
private void chk_QueryCheckStateByValue(object sender, DevExpress.XtraEditors.Controls.QueryCheckStateByValueEventArgs e)
{
if (e.Value == null)
{
e.CheckState = CheckState.Unchecked;
e.Handled = true;
}
}
EDIT: I created a List<bool> chkList; and do the following operations:
This function is added to checkedits' CheckStateChanged:
private void chk_CheckStateChanged(object sender, EventArgs e)
{
CheckEdit chk = sender as CheckEdit;
if (chk.Checked)
chkList[gvIsEmirleri.FocusedRowHandle] = true;
else
chkList[gvIsEmirleri.FocusedRowHandle] = false;
FillBindingSource();
}
In FillBindingSource I added the lines:
for (int i = 0; i < chkList.Count; i++)
{
if (chkList[i])
gvIsEmirleri.SetRowCellValue(i, "Sec", true);
}
I debug these lines, I see that List has correct bool values and gvIsEmirleri.SetRowCellValue(i, "Sec", true); is operated when it has to. However, it still doesn't work.
My guess is : You are using an unbound Column, and you are not saving the checked / unckecked info, so, after the selected row is left, the checkBox get it's initial value (unckecked).
For this, I suggest you handle the CustomUnboundColumnData event of your view. Here is a simple :
readonly Dictionary<object, bool> checkedMap = new Dictionary<object, bool>();
private void viewScales_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
{
// Check what column
if (e.Column != gvIsEmirleri.Columns["Sec"])
return;
if (e.IsGetData)
{
// check if the row has been checked and set it's value using e.Value
bool checked;
if (checkedMap.TryGetValue(e.Row, out checked))
e.Value = checked;
}
if (e.IsSetData)
{
var checked = Convert.ToBoolean(e.Value);
// Check if the key already exist
if (checkedMap.ContainsKey(e.Row))
scaleMap.Remove(e.Row);
checkedMap.Add(e.Row, checked);
}
}
Note : This is the way I resolved a similar problem, but I did not test the code I just wrote.
I have a GridView control of xtraGrid suite in a form.
When I open the form for first time it is AllowEdit = false. I want that when I press on add new row link(built in by control) to make editable this only new inserted row. I read that I should use ShowingEditor event but I don't know how.
I wrote this so far but this does not editable the row:
private void gridViewNote_ShowingEditor(object sender, System.ComponentModel.CancelEventArgs e)
{
//this is first tryout
//if (gridViewNote.IsNewItemRow(gridViewNote.FocusedRowHandle))// == gridViewNote.GetFocusedDataRow())
//{
// gridColumnStagione.OptionsColumn.AllowEdit = true;
//}
//second tryout
GridView view = sender as GridView;
SchedeMaterialiDaTaglioDS.SMTAGL_NOTERow currentRow = gridViewNote.GetFocusedDataRow() as SchedeMaterialiDaTaglioDS.SMTAGL_NOTERow;
SchedeMaterialiDaTaglioDS.SMTAGL_NOTEDataTable changesTable = dsSchMatTaglio.SMTAGL_NOTE.GetChanges() as SchedeMaterialiDaTaglioDS.SMTAGL_NOTEDataTable;
e.Cancel = !view.IsNewItemRow(view.FocusedRowHandle) &&
!changesTable.Contains(currentRow);// set.Inserts.Contains(order);
}
I hope I understood your question. A few simple ways of doing this:
Adding a repository item to each column and handle the ShowingEditor event, using e.Cancel if this is supposed to be read only.
Popping up a window/textboxes, letting the user insert values and add the row with values already inserted via code.
assigning two different repository items to the same column using gridView.CustomRowCellEdit event. like such:
RepositoryItemTextEdit rep = new RepositoryItemTextEdit();
RepositoryItemTextEdit noRep = new RepositoryItemTextEdit();
noRep.ReadOnly = true;
private void button1_Click(object sender, EventArgs e)
{
gridView1.AddNewRow();
justAddedName = true;
gridView1.RefreshData();
}
private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
if (e.Column == colname)
{
if (e.RowHandle == gridView1.RowCount - 1 && justAddedName)
{
e.RepositoryItem = rep;
}
else
{
e.RepositoryItem = noRep;
}
}
}
It's not complete, just a direction to explore.
Hope I helped.