DataGridView - AllowUserToDeleteRows - c#

Hey people I'm having trouble to deny row deletion if the value of a cell equals 'Y'.
I tried with:
for (int i = 0; i < dGridView.Rows.Count; i++)
{
dGridView.Rows[i].Cells["A"].ReadOnly = true;
dGridView.Columns["A"].DefaultCellStyle.BackColor = Color.Gray;
dGridView.Rows[i].Cells["B"].ReadOnly = true;
dGridView.Columns["B"].DefaultCellStyle.BackColor = Color.Gray;
if (dGridView.Rows[i].Cells["A"].Value != null && dGridView.Rows[i].Cells["A"].Value.Equals("Y"))
{
dGridView.Rows[i].Cells["F"].ReadOnly = true;
dGridView.Rows[i].Cells["F"].Style.BackColor = Color.Gray;
dGridView.Rows[i].Cells["C"].ReadOnly = true;
dGridView.Rows[i].Cells["C"].Style.BackColor = Color.Gray;
dGridView.Rows[i].Cells["U"].ReadOnly = true;
dGridView.Rows[i].Cells["U"].Style.BackColor = Color.Gray;
**** dGridView.AllowUserToDeleteRows = false;
**** }
Could you kindly support me?
Thanks!
I know that:
dGridView.AllowUserToDeleteRows = false;
will deny deletion of all rows, but I can't find the solution.

Related

Click event restart

I'm using c# windows form in Visual studio 2017 version
I've made a high low game for practice and it works perfect but what I'm trying to accomplish is once someone hits 10 guesses it starts the application over again.
This was my last resort coming here and asking but i have no one else to turn to. I've tried all kinds of things to make it work when hitting 10 guesses to restart the app but nothing works and i get errors.
here's the start button and the guess button
variables I'm using
static int intRandomNumber;
static int intNumGuesses;
static int intBestLowScore;
static int intGuessedNum;
static int difference = 0;
Start Button
{
//Random Numbers//
Random rnRandomNumber = new Random();
intRandomNumber = rnRandomNumber.Next(0, 1000);
lblRandomNumber.Text = intRandomNumber.ToString();
txtGuess.Enabled = true;
btnGuess.Enabled = true;
btnStart.Enabled = false;
lblAnswer.Text = "".ToString();
intBestLowScore = intNumGuesses;
intNumGuesses = 0;
lblNumGuesses.Text = "0";
lblBestScore.Text = intBestLowScore.ToString();
lblAnswer.BackColor = Color.White;
txtGuess.Focus();
SoundPlayer audio = new SoundPlayer(High_Low_Game.Properties.Resources.Cheering);
audio.Stop();
}
Guess Button
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
lblAnswer.Text = "To High";
lblAnswer.ForeColor = Color.Red;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else if (intRandomNumber - intGuessedNum > difference)
{
lblAnswer.Text = "To Low";
lblAnswer.ForeColor = Color.Blue;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else
{
lblAnswer.Text = "You Guessed it.";
lblAnswer.ForeColor = Color.Black;
lblAnswer.BackColor = Color.Green;
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
SoundPlayer audio = new SoundPlayer(High_Low_Game.Properties.Resources.Cheering);
audio.Play();
}
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
You're missing an if statement
Guess Button:
private void btnGuess_Click(object sender, EventArgs e)
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
//This is what you're looking for-v
if(intNumGuesses==10)
{
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
intNumGuesses=0;
}
//This is what you're looking for-^
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
lblAnswer.Text = "To High";
lblAnswer.ForeColor = Color.Red;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else if (intRandomNumber - intGuessedNum > difference)
{
lblAnswer.Text = "To Low";
lblAnswer.ForeColor = Color.Blue;
lblAnswer.BackColor = Color.White;
txtGuess.Text = "";
txtGuess.Focus();
}
else
{
lblAnswer.Text = "You Guessed it.";
lblAnswer.ForeColor = Color.Black;
lblAnswer.BackColor = Color.Green;
btnGuess.Enabled = false;
txtGuess.Enabled = false;
txtGuess.Text = "";
btnStart.Enabled = true;
}
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
In your 'guess-button' handler do the following:
{
intNumGuesses++;
lblNumGuesses.Text = intNumGuesses.ToString();
bool guessed = false;
try
{
intGuessedNum = Convert.ToInt32(txtGuess.Text);
if (intRandomNumber - intGuessedNum < difference)
{
...
}
else if (intRandomNumber - intGuessedNum > difference)
{
...
}
else
{
...
guessed = true;
}
if ((intNumGuesses == 10}&&(!guessed))
{
// Show Message "max. nr. of guesses reached'
// call method to clear values from textBoxes & enable Start-button
}
catch
{
MessageBox.Show("Input your Guess again and Integers Only. Retry.");
txtGuess.Focus();
}
}
What about this?
{
intNumGuesses++;
if(intNumGuesses >= 10)
{
StartButton(); // pseudo call, replace with whatever the name and parameters of your method really are
return;
}
lblNumGuesses.Text = intNumGuesses.ToString();
// ...
}
I would recommend to write an Init()method, that you call either from your StartButton() method or from your GuessButton() method when necessary

Telerik Radgridview Winforms Change Cell/Row background color

I am simply wanting to run a validation check fired from a button event on the form. For each row inside the grid, it checks the first cell to see if the value matches a string. For every one that fails, i'm wanting it to either change the cell or the row background color to be RED. When the user corrects the cell value and hit the button again, it will reverse back to the default color. For the life of me i can't get it to change the color. I know there is an event for cellformatting but surely i do not have to go through an event just to alter the style of a cell/row. I am trying the below code without any luck. All the examples i'v seen are event based so hoping i can avoid this entirely.
if (!ValidateParametersExistInScript(rtxtQuery.Text))
{
GridViewCellInfo cell = rgvNewParameters.Rows[0].Cells["ParameterName"];
cell.Style.BackColor = Color.Red;
cell.Style.DrawFill = true;
MessageBox.Show("Parameters do not match definition inside query, " +
"please check spelling and try again.");
}
You must first set CustomizeFill to true before you can modify the back color of the GridViewCellInfo.
if (!ValidateParametersExistInScript(rtxtQuery.Text))
{
GridViewCellInfo cell = rgvNewParameters.Rows[0].Cells["ParameterName"];
cell.Style.CustomizeFill = true;
cell.Style.BackColor = Color.Red;
cell.Style.DrawFill = true;
MessageBox.Show("Parameters do not match definition inside query, " +
"please check spelling and try again.");
}
private void radGridLedgerAccount_CellFormatting(object sender, CellFormattingEventArgs e)
{
if (e.RowIndex == 0 || e.RowIndex==radGridLedgerAccount.Rows.Count-1)
{
e.CellElement.DrawFill = true;
e.CellElement.ForeColor = Color.White;
e.CellElement.NumberOfColors = 1;
e.CellElement.BackColor = Color.Red;
}
else
{
if (e.CellElement.ColumnInfo.Name == "months") //checking for the text in header cell
{
if (e.RowIndex != 0)
{
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#6599FF");
e.CellElement.DrawFill = true;
e.CellElement.ForeColor = Color.White;
e.CellElement.NumberOfColors = 1;
e.CellElement.BackColor = col;
}
}
else
{
e.CellElement.DrawFill = true;
e.CellElement.ForeColor = Color.Black;
e.CellElement.NumberOfColors = 1;
e.CellElement.BackColor = Color.White;
e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
}
}

Dev express Grid View – Column Visible Index property giving un expected output

I have dev express grid view, and trying to set column visibility run time, Some columns are not appearing in correct order i.e. RebateAmount i want to set to appear at 4th position but its appearing always on second position, any idea what's wrong in below code? i want all columns should be appear at position which i have set in visibility index.
if (currentColum.FieldName.Equals("TaxName"))
{
currentColum.Caption = #"abc";
}
else if (intGridType == 1 || intGridType == 0)//Both
{
if (currentColum.FieldName.Equals("PastCurrentCollectionTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 15;
}
else if (currentColum.FieldName.Equals("PastCurrentCollectionVyajTotal"))
{
currentColum.Caption = #"Äyij";
currentColum.VisibleIndex = 16;
}
else if (currentColum.FieldName.Equals("PastCurrentCollectionNoticeFeeTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 17;
}
else if (currentColum.FieldName.Equals("RebateAmount"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 18;
}
else if (currentColum.FieldName.Equals("PastCurrentCollectionTotalTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 19;
}
else
currentColum.Visible = false;
}
else if (intGridType == 2)//Only Past
{
if (currentColum.FieldName.Equals("PastCollection"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 6;
}
else if (currentColum.FieldName.Equals("PastCollectionVyaj"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 7;
}
else if (currentColum.FieldName.Equals("PastCollectionNoticeFee"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 8;
}
else if (currentColum.FieldName.Equals("PastCollectionTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 9;
}
else
currentColum.Visible = false;
System.Diagnostics.Debug.Print(currentColum.VisibleIndex.ToString() + currentColum.Name);
}
else if (intGridType == 3) //Only Current
{
if (currentColum.FieldName.Equals("CurrentCollection"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 10;
}
else if (currentColum.FieldName.Equals("CurrentCollectionVyaj"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 11;
}
else if (currentColum.FieldName.Equals("CurrentCollectionNoticeFee"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 12;
}
else if (currentColum.FieldName.Equals("CurrentCollectionTotal"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 13;
}
else if (currentColum.FieldName.Equals("RebateAmount"))
{
currentColum.Visible = true;
currentColum.VisibleIndex = 14;
}
else
currentColum.Visible = false;
}
Hide all columns and active ( Visible = true) one at a time in order you want

how to increase speed of tchart refresh()?

I have 16 graphs[maximum ] with 4 fastlines in each graph. In each graph 3 fastlines represent min , max and ideal value. 4th fastline is actual values from the hardware. I have to run the test for 18,000 samples. So , first 3 fastlines are already drawn and when the switch is on and data comes in , 4th fastline is drawn. In order to draw the 4th line, I use the method Series4.Add(actualvalue, "" , color.red) .
here is the problem. each time the sample is drawn on 4th line. chart has to be refreshed in order to view the plotting of that sample. that also redraws the other 3 fastlines with 18,000 samples in each . so it redraws that many samples without use again and again. I need to draw only 4th fastline.
I can not use an array to fill the values up and then assign it as a source of fastline because I dont have any values beforehand. I tried series4.repaint() method and series4.refreshseries() method, but that doesnt repaint 4th series actually. we have to refresh the whole chart.
and therefore, it slows down the performance because of high number of samples in each fastline [18,000] and one graph with 4 fastlines and total 16 graphs like this.
I ve already done
Series4.AutoRepaint = false, Series4.DrawAllPoints = false;
Series4.XValues.Order = ValueListOrder.None , Series4.YValues.Order = ValueListOrder.None
Is there any way I can increase the performance ?
Thank you.
I have made a simple code, where I have added 4 charts with 4 FastLines with 18000 points for each fastline using a table to add a initialize values and after I update only the values of Series4. The performance is good for the number of values I am drawing:
public Form1()
{
InitializeComponent();
InitializeChart();
}
// Steema.TeeChart.Styles.FastLine Series1;
Timer timer1, timer2,timer3, timer4;
Random r;
DateTime dt;
DateTime[] Xvalues1;
double[] Yvalues1;
Steema.TeeChart.TChart tChart1, tChart2, tChart3,tChart4;
private void InitializeChart()
{
tChart1 = new TChart();
tChart2 = new TChart();
tChart3 = new TChart();
tChart4 = new TChart();
this.Controls.Add(tChart1);
this.Controls.Add(tChart2);
this.Controls.Add(tChart3);
this.Controls.Add(tChart4);
//Initialize Locations and size
this.Width = 908;
this.Height = 600;
//Location
tChart1.Left = 12;
tChart1.Top = 53;
tChart2.Left = 468;
tChart2.Top = 53;
tChart3.Left = 12;
tChart3.Top = 318;
tChart4.Left = 468;
tChart4.Top = 318;
//Size
tChart1.Width = 373;
tChart1.Height = 236;
tChart2.Width = 373;
tChart2.Height = 236;
tChart3.Width = 373;
tChart3.Height = 236;
tChart4.Width = 373;
tChart4.Height = 236;
tChart1.Aspect.View3D = false;
tChart2.Aspect.View3D = false;
tChart3.Aspect.View3D = false;
tChart4.Aspect.View3D = false;
tChart1.Legend.Visible = false;
tChart2.Legend.Visible = false;
tChart3.Legend.Visible = false;
tChart4.Legend.Visible = false;
tChart1.Panel.Gradient.Visible = false;
tChart2.Panel.Gradient.Visible = false;
tChart3.Panel.Gradient.Visible = false;
tChart4.Panel.Gradient.Visible = false;
tChart1.Axes.Bottom.AxisPen.Visible = false;
tChart2.Axes.Bottom.AxisPen.Visible = false;
tChart3.Axes.Bottom.AxisPen.Visible = false;
tChart4.Axes.Bottom.AxisPen.Visible = false;
tChart1.Axes.Left.AxisPen.Visible = false;
tChart2.Axes.Left.AxisPen.Visible = false;
tChart3.Axes.Left.AxisPen.Visible = false;
tChart4.Axes.Left.AxisPen.Visible = false;
//Series
tChart1.AutoRepaint = false;
tChart2.AutoRepaint = false;
tChart3.AutoRepaint = false;
tChart4.AutoRepaint = false;
for (int i = 0; i < 4; i++)
{
new Steema.TeeChart.Styles.FastLine(tChart1.Chart);
new Steema.TeeChart.Styles.FastLine(tChart2.Chart);
new Steema.TeeChart.Styles.FastLine(tChart3.Chart);
new Steema.TeeChart.Styles.FastLine(tChart4.Chart);
tChart1[i].XValues.DateTime=true;
tChart2[i].XValues.DateTime = true;
tChart3[i].XValues.DateTime = true;
tChart4[i].XValues.DateTime = true;
InitialDataSeries(tChart1[i]);
InitialDataSeries(tChart2[i]);
InitialDataSeries(tChart3[i]);
InitialDataSeries(tChart4[i]);
}
//Axes labels
tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd/MM";
tChart1.Axes.Bottom.Labels.Angle = 90;
tChart2.Axes.Bottom.Labels.DateTimeFormat = "dd/MM";
tChart2.Axes.Bottom.Labels.Angle = 90;
tChart3.Axes.Bottom.Labels.DateTimeFormat = "dd/MM";
tChart3.Axes.Bottom.Labels.Angle = 90;
tChart4.Axes.Bottom.Labels.DateTimeFormat = "dd/MM";
tChart4.Axes.Bottom.Labels.Angle = 90;
tChart1.AutoRepaint = true;
tChart2.AutoRepaint = true;
tChart3.AutoRepaint = true;
tChart4.AutoRepaint = true;
tChart1.Refresh();
tChart2.Refresh();
tChart3.Refresh();
tChart4.Refresh();
//Timer
timer1 = new Timer();
timer1.Start();
timer1.Interval = 100;
timer1.Tick += new EventHandler(timer1_Tick);
}
void timer1_Tick(object sender, EventArgs e)
{
//See the chart data updated.
tChart1[0].Visible = false;
tChart1[1].Visible = false;
tChart1[2].Visible = false;
PopulateSeries(tChart1[3]);
PopulateSeries(tChart2[3]);
PopulateSeries(tChart3[3]);
PopulateSeries(tChart4[3]);
}
private void PopulateSeries(Steema.TeeChart.Styles.Series Series1)
{
r = new Random();
dt = DateTime.Now;
tChart1.AutoRepaint = false;
tChart2.AutoRepaint = false;
tChart3.AutoRepaint = false;
tChart4.AutoRepaint = false;
// show only 50 points - delete the rest
while (Series1.Count > 10000)
{
Series1.Delete(0);
}
if (Series1.Count > 10000)
{
Series1.Delete(0);
}
else
{
for (int t = 0; t < 100; t++)
{
Series1.Add(dt, r.Next(1000));
dt = dt.AddSeconds(15);
}
}
tChart1.AutoRepaint = true;
tChart2.AutoRepaint = true;
tChart3.AutoRepaint = true;
tChart4.AutoRepaint = true;
tChart1.Refresh();
tChart2.Refresh();
tChart3.Refresh();
tChart4.Refresh();
}
private void InitialDataSeries(Steema.TeeChart.Styles.Series Series1)
{
//Arrays
dt = DateTime.Now;
r = new Random();
Xvalues1 = new DateTime[18000];
Yvalues1 = new double[18000];
(Series1 as Steema.TeeChart.Styles.FastLine).DrawAllPoints = false;
for (int j = 0; j < 18000; j++)
{
Xvalues1[j] = dt;
dt = dt.AddSeconds(15);
Yvalues1[j] = r.Next(1000);
}
Series1.Add(Xvalues1, Yvalues1);
}
Could you tell us if it help you? On the other hand, if my code doesn't help you I recommend you use TeeChartDirect2D, this is ideal for the high speed data throughput required by DSP realtime applications. See the White paper, Boosting graphics-rendering performance in Windows Forms, for a closer look.
Thanks,

Why do my X-Axes labels disappear when I zoom using MS Chart Controls?

I'm using MS Chart Controls. The line chart is a normal time based chart. The problem is when I click the chart and select some time it zooms in, the scrollbar appears, and the x-axes labels disappears. How can I prevent this from happening? If I cannot fix it automatically, is there code I can add to a button that will fix the labels?
private void Chart0Configuration()
{
chart1.ChartAreas[0].Visible = false;
chart1.ChartAreas[0].AlignmentOrientation = AreaAlignmentOrientations.Vertical;
chart1.ChartAreas[0].AlignmentStyle = AreaAlignmentStyles.All;
chart1.ChartAreas[0].Position.Auto = false;
chart1.ChartAreas[0].Position.X = 2;
chart1.ChartAreas[0].Position.Y = 10;
chart1.ChartAreas[0].Position.Width = 98;
//chart1.ChartAreas[0].Position.Height = *****variable
//chart1.ChartAreas[0].AxisY.MajorTickMark.Enabled = false;
//chart1.ChartAreas[0].AxisY.MinorTickMark.Enabled = false;
//chart1.ChartAreas[0].AxisY.MajorGrid.Enabled = false;
//chart1.ChartAreas[0].AxisX.MajorTickMark.Enabled = true;
//chart1.ChartAreas[0].AxisX.MinorTickMark.Enabled = true;
//chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = true;
//chart1.ChartAreas[0].AxisX.MajorTickMark.Interval = 10;
chart1.ChartAreas[0].AxisX.Interval = 0;
chart1.ChartAreas[0].AxisX.IntervalAutoMode = IntervalAutoMode.VariableCount;
chart1.ChartAreas[0].AxisX.LabelStyle.Enabled = true;
//chart1.ChartAreas[0].AxisX.LabelStyle.Format = "hh:mm:ss";
chart1.ChartAreas[0].AxisY.LabelStyle.IsEndLabelVisible = true;
chart1.ChartAreas[0].InnerPlotPosition.Auto = false;
chart1.ChartAreas[0].InnerPlotPosition.X = 3;
chart1.ChartAreas[0].InnerPlotPosition.Y = 10;
chart1.ChartAreas[0].InnerPlotPosition.Width = 88;
chart1.ChartAreas[0].InnerPlotPosition.Height = 80;
chart1.ChartAreas[0].CursorX.IsUserEnabled = true;
chart1.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
//chart1.ChartAreas[0].CursorX.AutoScroll = true;
chart1.ChartAreas[0].CursorX.Position = 0;
chart1.ChartAreas[0].CursorX.Interval = 0;
chart1.ChartAreas[0].AxisX.ScrollBar.Size = 5;
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
chart1.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
chart1.ChartAreas[0].AxisX.ScrollBar.BackColor = Color.LightGray;
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonColor = Color.Gray;
chart1.ChartAreas[0].AxisX.ScrollBar.LineColor = Color.Black;
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
}
I inadvertently found the answer when looking at the intervals. They stay visible now that I've add the following line of code.
chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Days;

Categories