my code seems to be not working
gridView6.Columns["QueueID"].OptionsColumn.AllowEdit = true;
I also tried
int n = Convert.ToInt32(gridView6.Columns.Count.ToString());
for (int i = 0; i < n; i++)
{
gridView6.Columns["QueueID"].OptionsColumn.AllowEdit = true;
}
I have tried these codes but my gridview still does not allow editing after executing my winform, Is there other way to make this work? because my code does not catch any errors
AllowEdit is in effect if the View's ColumnView.Editable property is set to true.
Check if you enabled ColumnViewOptionsBehavior.Editable Property.
Related
I have a following scenario , in which I am dynamically creating bunch of controls in the panel and I have also the feature of removing and adding controls in the panel later on.
Following controls , I want to remove from the panel using the selection of the checkbox that is inside the each of the subcontrol .
Removal Code
Using the Idea mentioned in Stackoverflow , I avoided foreach loop and came up with the following code.
for (int i = 0; i < panelDetection.Controls.Count; i++)
{
if (panelDetection.Controls[i] is ImageControl)
{
ImageControl imageControl = (ImageControl)panelDetection.Controls[i];
for(int j = 0; j < imageControl.Controls.Count; j++)
{
if (imageControl.Controls[j] is CheckBox)
{
CheckBox chkBox = (CheckBox)(imageControl.Controls[j]);
if (chkBox.Checked)
{
chkBox.Click -= CheckBox_Click;
panelDetection.Controls.Remove(imageControl);
imageControl.Dispose();
}
}
}
}
}
But the result is very strange as the controls are not getting removed of from the panel in an expected way. You can see that all selected controls are not getting removed from the panel.
Is there any way to achieve this functionality using some other idea?
Good day!
I have read a lot about dynamic cells back color changing, but have not found the resolve of my problem.
In my project, I used 32x32 DataGridView. All values are integer, also I've changed default cell format to Numeric.
This code makes gradient painting without any problem:
void AFRTableGraient()
{
for (int i = 0; i < dataGridViewTAF.ColumnCount; i++)
{
for (int j = 0; j < dataGridViewTAF.RowCount; j++)
{
try
{
if (dataGridViewTAF.Rows[i].Cells[j].Value == null)
{
break;
}
dataGridViewTAF.Rows[i].Cells[j].Style.BackColor = Color.FromArgb(0x10 + (int)dataGridViewTAF.Rows[i].Cells[j].Value, 0x88 + (int)dataGridViewTAF.Rows[i].Cells[j].Value, 0x88 + (int)dataGridViewTAF.Rows[i].Cells[j].Value);
}
catch
{
break;
}
}
}
dataGridViewTAF.Update();
dataGridViewTAF.Refresh();
}
But I need to change cell color after value changed.
I've used CellFormatting, KeyDown, CellLeave, CellPainting, Enter, etc, events, but has no result.
When I tried to use CellPainting method, the app is freeze, but update the color is not happening.
Also, I've tried to use the "timer", but still nothing.
I know, there is a simple way of resolving, but I need help to found it.
Thanks!
I have a WPF window with a maintabWindow and several tabitems.
It normally works fine and the layout is this:
but when I BEFORE add the following window:
the result is this:
So the problem is related with the tabControl/tabItem refresh.
This is fairly obvious but even more because if I move the window or pass with the mouse on the a tabItem they get refreshed one by one.
I searched and found that here is a solution: http://geekswithblogs.net/NewThingsILearned/archive/2008/08/25/refresh--update-wpf-controls.aspx
so I added:
this.MainTab.Refresh();
this.tabItem1.Refresh();
this.tabItem2.Refresh();
this.tabItem3.Refresh();
this.tabItem4.Refresh();
this.tabItem5.Refresh();
but that didn't change a thing.
Thanx for any help
Ok so in the end it has a quite a weird behavious. If I do
for (int i = 0; i < tbcMain.Items.Count; i++)
{
tbcMain.SelectedIndex = i;
tbcMain.UpdateLayout();
}
it works. But I have to set the 1st tabitem so if I add
tbcMain.SelectedIndex = 0;
it doesn't.
So the solution was put a sleep and it works again.
for (int i = 0; i < tbcMain.Items.Count; i++)
{
tbcMain.SelectedIndex = i;
tbcMain.UpdateLayout();
}
System.Threading.Thread.Sleep(250);
tbcMain.SelectedIndex = 0;
But that is not elegant at all. If anyone has a better solution pls let me know it.
Btw adding the tbcMain.SelectedIndex = 0; on the loaded event of the mainWindow is of no use.
You should be able to set your SelectedIndex first, and without having to include it in your loop:
tbcMain.SelectedIndex = 0;
Then, basing it off of your response, you should be able to either just do .UpdateLayout() on each of your TabItems:
MainTab.UpdateLayout();
tabItem1.UpdateLayout();
tabItem2.UpdateLayout();
tabItem3.UpdateLayout();
tabItem4.UpdateLayout();
tabItem5.UpdateLayout();
Or you should be able to do something like this in your loop:
MainTab.UpdateLayout();
for (int i = 0; i < tbcMain.Items.Count; i++)
{
TabItem tbi = (TabItem)this.FindControl("tabItem"+i);
tbi.UpdateLayout();
}
Updating/refreshing should have nothing to do with setting the selected one. Including the selection of the tab within the loop to i was your problem - not a race condition. Set the tbcMain.SelectedIndex = 0 outside of your loop and you should be fine. Sometimes, however, this doesn't work and you need to set it with Dispatcher:
Dispatcher.BeginInvoke((Action)(() => this.tbcMain.SelectedIndex = 0));
There's a write up/comments on a separate thread regarding why it needs to be sent to Dispatcher:
How to programmatically select a TabItem in WPF TabControl
Though, unfortunately for me, I had a similar issue where I was trying to refresh a ListView on a subtab. Neither .UpdateLayout(), nor .InvalidateVisual() (as I saw on this thread) worked. I just had to rebind my grid in the button event I was using on my main page, so that when the tab was clicked, it was refreshed manually. I added an x:Name property on the tab so I could call it using "dot" syntax, and it exposed the ListView. I simply added the DataTable of results back to that ListView's DataContext.
I have a datagridview showing installments of a loan. I created a datagridviewcheckbox column so then I can select all the installments i want to pay for.
This is a screen of the datagrid:
My issue is that I need to disable the checkboxes of the paid intallments. In this case, when "Restante" (what´s left to pay) is = 0.
I read some posts where they used the paint event to not show the checkbox cell, but i didnt like that solution. I thought of hiding the checkbox cell, but i don´t know if it is possible to do that.
Thats what i tried:
foreach (DataGridViewRow row in dgv_Cuotas.Rows)
{
if (Convert.ToDecimal(dgv_Cuotas.Rows[row.Index].Cells[17].Value) == 0)
{
dgv_Cuotas.Rows[row.Index].Cells[16].Visible = false;
}
}
Obviously this does not works, I get a compiler error message saying that the property is read only.
Does somebody knows how to set the checkbox cell to invisible?
Just in case, I attach the DataGridViewCheckboxColumn creation code:
DataGridViewCheckBoxColumn chbox = new DataGridViewCheckBoxColumn();
{
chbox.CellTemplate = new DataGridViewCheckBoxCell();
chbox.HeaderText = "";
chbox.Name = "Seleccionar";
chbox.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
chbox.FlatStyle = FlatStyle.Standard;
}
dgv_Cuotas.Columns.Insert(16, chbox);
dgv_Cuotas.Columns[16].DisplayIndex = 0;
EDIT:
Some considerations:
I use the cell content click event to handle the checkboxes, so readonly wont work. What I want is to hide the checkbox:
private void dgv_Cuotas_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1)
return;
if (dgv_Cuotas.Columns[e.ColumnIndex].Name == "Seleccionar")
{
DataGridViewRow row = dgv_Cuotas.Rows[e.RowIndex];
DataGridViewCheckBoxCell cellSeleccion = row.Cells["Seleccionar"] as DataGridViewCheckBoxCell;
int n_cuota = Convert.ToInt32(dgv_Cuotas[2, dgv_Cuotas.CurrentRow.Index].Value);
Cuota cuota_seleccionada = new Cuota();
cuota_seleccionada = Lista_cuotas.Where(x => x.num_cuota == n_cuota).First();
if (cellSeleccion != null && Convert.ToBoolean(cellSeleccion.Value) == true)
{
cellSeleccion.Value = false;
Actualizar_cuotas_seleccionadas(false, cuota_seleccionada);
}
else
{
if (cellSeleccion != null && Convert.ToBoolean(cellSeleccion.Value) == false)
{
cellSeleccion.Value = true;
Actualizar_cuotas_seleccionadas(true, cuota_seleccionada);
}
}
}
In the other hand, I´m already using the Onpaint event. Its inherited, thats why I´m trying to avoid using it.
Assign a value to the checkbox cell. Then Convert it to a TextBox with a new value.
Worked for me.
dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";
Yes, you can do this by Converting the DataGridViewCheckBoxCell to DataGridViewTextBoxCell
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (dataGridView1.Rows[row.Index].Cells[17].EditedFormattedValue.ToString().Length == 0) // if (string.IsNullOrWhiteSpace(dataGridView1.Rows[row.Index].Cells[4].EditedFormattedValue.ToString()))
break;
if (Convert.ToDecimal(dataGridView1.Rows[row.Index].Cells[17].EditedFormattedValue) == 0)
{
dataGridView1.Rows[row.Index].Cells[16].Value = null;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
}
else
{
//dgv_Cuotas.Rows[row.Index].Cells[16] = new DataGridViewCheckBoxCell();
}
}
Use the cell's ReadOnly attribute to disable any modification.
If you want to turn it to hidden, you need to override the painting code for the cells.
Try to hide it and the value remains, which should prevent runtime errors.
dataGridView1.Rows[row.Index].Cells[16].Style.Padding =
new Padding(dataGridView1.Rows[row.Index].Cells[16].OwningColumn.Width, 0, 0, 0);
I took spajce's answer and tweaked it a bit to make it work for me.
for (var i = 0; i < datagridview1.Count; i++)
{
if ((bool)datagridview1[0, i])
{
datagridview1[0, i] = new DataGridViewTextBoxCell
{
Style = { ForeColor = Color.Transparent,
SelectionForeColor = Color.Transparent }
};
}
}
We're basically iterating through the rows and looking for a 'true' checked box. If it's checked then we're converting the box to a text box and setting its text color to Transparent so the cell looks empty. I hope this helps everyone who had this problem, I spent hours trying to find a workable answer.
There are several ways to accomplish what you want.
For example, you can use the Readonly property of the cell to avoid the user to change the values and change the appeareance of the control to look grayed:
C# DataGridViewCheckBoxColumn Hide/Gray-Out
A simple and effective alternative is to use the chkbox ThreeState property. In code below, if my object does not have an email address, then I don't want to allow the user to tick the checkbox for sending email. To do this, the check box value is set to Unknown and read-only, which means it is displayed as "unselectable" and user cannot modify.
chk = DirectCast(row.Cells(m_idxEmailChecked), DataGridViewCheckBoxCell)
If String.IsNullOrEmpty(contact.EmailAddress) Then
chk.Value = enumTristate.Unknown
chk.ReadOnly = True
Else
chk.ThreeState = False
End If
Note this also requires several associated ThreeState properties to be set on the checkbox.
.ValueType = GetType(enumTristate)
.TrueValue = enumTristate.True
.FalseValue = enumTristate.False
.IndeterminateValue = enumTristate.Unknown
.ThreeState = True
Good luck.
I tried Charlie's way:
dataGridView1.Rows[row.Index].Cells[16].Value = false;
dataGridView1.Rows[row.Index].Cells[16] = new DataGridViewTextBoxCell();
dataGridView1.Rows[row.Index].Cells[16].Value = "";
I got fewer errors, but still kept getting them. So I tried to instantiate the new DataGridViewTextBoxCell separately, and that did the trick for me (I didn't need to set the checkbox cell value either):
DataGridViewTextBoxCell c = new DataGridViewTextBoxCell();
c.Value = "";
dataGridView1.Rows[row.Index].Cells[16] = c;
Hope that helps someone!
On one web user control
public void displayFindingSection(int sectionsid,string text,string head)
{
SectionHeading.Text = head;
DataSet totImgs;
totImgs = objGetBaseCase.GetFindingsNewerImages(sectionsid);
FindingViewerlist.DataSource = totImgs;
DataBind();
SectionText.Text = text;
}
On other web user control
public void DisplayFindingsViewer(CipCaseWorkflowItem2 item)
{
FindingViewerDisplay.Visible = true;
ImageAndSimpleViewer.Visible = false;
objGetBaseCase.GetFindingsImages((Convert.ToInt32(Session["CaseId"])), item.ItemId);
FindingsViewerNew = objGetBaseCase.GetFindingViewerNewElementDetails(item.ItemId);
for (int i = 0; i < FindingsViewerNew.Count; i++)
{
FindingViwerDisplay uc = (FindingViwerDisplay)LoadControl("FindingViwerDisplay.ascx");
FindingPlaceholder.Controls.Add(uc);
uc.displayFindingSection(Convert.ToInt32(FindingsViewerNew[i].Index), FindingsViewerNew[i].Text, FindingsViewerNew[i].Title);
}
}
I am adding the all the image in user control and displaying the image, but when i am using the above code, web user control is also adding every time and one image is showing in in control what i want is all images should show in only one user control.. sectionsid is getting the image id from the database. I think prob with for loop but i am unable to solve it.. help me it that
Might be it is happening u have defined it inside the loop
FindingViwerDisplay uc = (FindingViwerDisplay)LoadControl("FindingViwerDisplay.ascx");
FindingPlaceholder.Controls.Add(uc);
On Each loop you are adding uc and calling displayFindingSection whcich ofcouse add 1 image than loop go back add a new control again and than add one image it will go on till your loop completion so add control once before loop and call just displayFindingSection in loop..
Do this,
FindingViwerDisplay uc = (FindingViwerDisplay)LoadControl("FindingViwerDisplay.ascx");
FindingPlaceholder.Controls.Add(uc);
//define here a dataTabel with three columns let say u have datatable dt
for (int i = 0; i < FindingsViewerNew.Count; i++)
{
dt.Rows.Add(Convert.ToInt32(FindingsViewerNew[i].Index), FindingsViewerNew[i].Text, FindingsViewerNew[i].Title);
}
uc.displayFindingSection(dt);
Then work out on that dt in displayFindingSection
Sorry if i am wrong...