C# Wrong Loop On My DataGridView - c#

Good day, I'm having an error. I choose the August 5, 2015 on the date picker with the loop count of 3. But I'm having a problem. I'm using add 15 prior to the given output. But the first count is wrong.
THE OUTPUT IN THE FIRST COUNT IS
8/20/2015
but it should be
8/05/2015 because I use the 8/05/2015 on the datepicker.
I don't know what's the problem here.
Here's my code (This will be execute when the button "Calculate Schedule" is click"
private void execute_Click(object sender, EventArgs e)
{
var fromDate = date_from.Value; // Getting the value from DatePicker
int count;
for (count = 0; count < 3; count++) {
dgv_result.Rows.Add(1);
int numrows = count + 1;
fromDate = fromDate.AddDays(15);
dgv_result.Rows[count].Cells[1].Value = numrows; // Just for numbering the rows
dgv_result.Rows[count].Cells[0].Value = fromDate.ToShortDateString();
}
}
Here's my Screenshot
NOTE: Just check the Month only

You just have to put this line at the end of the loop:
for (count = 0; count < 3; count++)
{
dgv_result.Rows.Add(1);
int numrows = count + 1;
dgv_result.Rows[count].Cells[1].Value = numrows; // Just for numbering the rows
dgv_result.Rows[count].Cells[0].Value = fromDate.ToShortDateString();
//add days after adding the row, so next line will be effected by it
fromDate = fromDate.AddDays(15);
}
I am considering you're not using fromDate anywhere else and it is just for the loop.

You are adding days before you write your first row. So you need to .AddDays(15) at the end line of for loop.

Related

how to create Schedule in c#

I'm new into programing and still learning. Currently I'm coding simple form for my schedule program (c#) in MS Visual Studio 2017. I already program day in month grid ( when you change date program generate gridview for it) but I have two important problem:
I would like to get day number in each cell, I tried use loop while and foreach but it doesn't work....
Second thing is that I would like to save my appointment in cell.
If its possible I would rather not use database, and I really don't want to use solutions that I would have to pay.
Here's my code:
namespace WindowsFormsApp1
{
public partial class Kalendarz : Form
{
public Kalendarz()
{
InitializeComponent();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DateTime date_start = this.dateTimePicker1.Value;
this.textBox2.Text = date_start.ToString("hh-mm-dd-MM-yyyy"); // czas startu
this.textBox1.Text = date_start.ToString("MMMM");
int Month = date_start.Month;
int Year = date_start.Year;
int NoD = DateTime.DaysInMonth(Year, Month);
Decimal NoWD = Decimal.Divide(NoD, 7.0m);
if (NoWD > 4.1m)
{
int WN = 5;
dataGridView1.RowCount = WN;
int n0 = 1;
foreach(DataGridCell cell in dataGridView1)
{
while (n0 = NoD; n0++)
}
}
else
{
int WN = 4;
dataGridView1.RowCount = WN;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
dataGridView1.Columns["s"].Visible = true;
dataGridView1.Columns["st"].Visible = true;
}
else if (!checkBox1.Checked)
{
dataGridView1.Columns["s"].Visible = false;
dataGridView1.Columns["st"].Visible = false;
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
Do you have any suggestions what can I do ?
I found something like that but I don't have any idea how to loop it
dataGridView.CurrentCell.Value = newValue.ToString ();
Be careful, what you whish for...
Regarding 1st request: This code will fill a DataGridView with day numbers, starting on the set day in week and not exceeding number of days in month, provided you have enough rows an columns, filling it from left to right (as opposed to i.e. top-down). I'd advice 7 columns, unless you want to have a lot of fun with calendar.
Guessing from your name, you're Polish and as far as I know week starts with Monday in Poland. You'd need a modification to the code for week starting with Sunday.
VB.NET:
Dim DayCounter As Int16 = 1
Dim DayInWeekStart As Int16 = 2 ' 1 = MON, 2 = TUE, 3 = WED, ...
Dim DayInMonthTotal As Int16 = 31
For ir = 0 To Me.dgv.rows.count - 1
For ic = 0 To Me.dgv.columns.count - 1 ' you should have 7 columns for MON-SUN in your DataGridView
If ir = 0 And (ic + 1) < DayInWeekStart And DayCounter < DayInMonthTotal Then
' skip / do nothing
Else
Me.dgv.rows(ir).cells(ic).value = DayCounter ' set DGV cell value to the date
DayCounter += 1 ' iterate day in month number
End If
Next
Next
C# (converted):
Int16 DayCounter = 1;
Int16 DayInWeekStart = 2;
// 1 = MON, 2 = TUE, 3 = WED, ...
Int16 DayInMonthTotal = 31;
For (ir = 0; ir <= this.dgv.rows.count - 1; ir++) {
// you should have 7 columns for MON-SUN in your DataGridView
For (ic = 0; ic <= this.dgv.columns.count - 1; ic++) {
If (ir == 0 & (ic + 1) < DayInWeekStart & DayCounter < DayInMonthTotal) Then {
// skip / do nothing
} else {
this.dgv.rows(ir).cells(ic).value = DayCounter;
// set DGV cell value to the date
DayCounter += 1;
// iterate day in month number
}
}
}
As for saving, the best option would be definitely a database, even though a structured text file is doable. If you have a lot of time...
For database, I'd use MERGE statement to UPDATE, DELETE or INSERT appointments.

How to get the column counts of StackedHeaders?

In my project, I have checked with many test cases and in one test case, I have dragged the stacked column header, I need to get the column counts belongs to that StackedHeader because I have to do some customization with their column index. Please anyone suggest me how to achieve this.
Thanks!
Define your code like:
private int RecursiveIteration(GridFieldCollection field) {
int count = 0;
for (int i = 0; i < field.Count ; i++)
{
if (field[i].GetType() == typeof(GroupField))
{
count += RecursiveIteration((field[i] as GroupField).Columns);
}
count++;
}
return count; }
To get total count: int colCount = RecursiveIteration(WebDataGrid1.Columns);

How do i Take out the last value character inside a ListView Column

I have a Column that looks like this:
Column 3:
-1
-2
-2a
-2b
etc...
try
{
int count = 0; int GetLength; string Result;
for (int i = 0; i < MMDetailsList.Items.Count; i++)
{
if (!string.IsNullOrEmpty(MMDetailsList.Items[count].SubItems[3].Text))
{
GetLength = MMDetailsList.Items[count].SubItems[3].Text.Length;
Result = Convert.ToString(MMDetailsList.Items[count].SubItems[3].Text[GetLength]);
MessageBox.Show(Result);
//getCategoryNum = MMDetailsList.Items[count].SubItems[3].Text;
}
count++;
}
}
catch (Exception) { }
What i'm doing is i wanted to know that if my series has a character at the end so i can increment it each time a button is clicked let say 2a if there's a character at its last value increment it to b and if b is deleted in row it would check again the last entry in the row a and would increament it back to b instead its c i need to check it 1st if it exist in my code i tried to check the last value of the column but it doesn't show my question is how do i check the last part character of the value inside the column listview i tried doing it with the code above but it's doesn't show a messagebox either.
for (int i = 0; i < MMDetailsList.Items.Count; i++)
{
string text = MMDetailsList.Items[i].SubItems[3].Text;
if (!string.IsNullOrEmpty(text))
{
if(char.IsLetter(text.Last()))
{
MessageBox.Show("Last Letter :" +text.Last());
}
}
}

c# sorting and getting the last row of datagridview

I want to sort the data in datagridview by numbers, and I want to display the specific cell of last row in a textbox. How can I do that?
my code : (EDITED)
if (dataGridView1.Rows.Count > 0)
{
int nRowIndex = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[nRowIndex].Selected = true;
textBox2.Text = dataGridView1.Rows[nRowIndex].Cells[2].Value.ToString() ; << exception occur
And i get this NullReferenceException.
How to solve this?
PS: I have 3 columns there, I need to get the 3rd columns of the last row.
You are getting exeption because you are trying to get the fourth cell (dataGridView1.Rows[nRowIndex].Cells[3]) and you have, as you said, only 3 cells(columns). Just change your code to:
textBox2.Text = dataGridView1.Rows[nRowIndex].Cells[2].Value.ToString()
textBox2.Text = dataGridView1.Rows[nRowIndex].Cells[dataGridView1.Columns.Count -1].Value.ToString() ;
Based on your last comment, I have changed your code a bit:
if (dataGridView1.Rows.Count > 0)
{
int nRowIndex = dataGridView1.Rows.Count - 1;
dataGridView1.Rows[nRowIndex].Selected = true;
if(dataGridView1.Rows[nRowIndex].Cells[2].Value != null)
textBox2.Text = dataGridView1.Rows[nRowIndex].Cells[2].Value.ToString() ; }
In page_load ()
write :-> gvJobCard.PageIndex = Int32.MaxValue;
protected void gvJobCard_RowDataBound(object sender,GridViewRowEventArgs e)
{
gvJobCard.PageIndex = gvJobCard.PageCount - 1; //It works for DESC order in gvJOBCARD
}

Listbox For statement not looping through listboxs

4th time asking this question after trying to apply so fixes. My problem is I have 2 listboxs one holds IDs and the other holds Cell ids. The ID listbox has 10 items (for testing) and Cell boxes have 5 ID. I'm trying to process listId and listCell at the same time creating update links...
example MakeReq will create txtWebUpdate.Text listID listCell &ire=1, in which is
store.domain.com/101/sec01&ire=1
store.domain.com/102/sec02&ire=1
store.domain.com/103/sec03&ire=1
store.domain.com/104/sec04&ire=1
store.domain.com/105/sec05&ire=1
store.domain.com/106/sec01&ire=1 <- notice how listCell starts over it
continues to loop applying sections until the ListID is complete.
here's the code i've been working with. what's happening with my code is that it's only selecting the first item. It's not going to the next item after its done.
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() +
"&ire=1", listCell.Items[b].ToString());
//System.Threading.Thread.Sleep(5);
}
}
}
Please only comment if you can help. I'm almost positive that the problem is the for statement and how it's selecting list items, but I may be wrong.
Edit More Info: after testing an example/suggestion for #duffp below the problem is definitely with my For statements. What's happening is its counting that I have 5 entries in ListCell and then output one (the same) ListID 5times but with a different ListCell. Can someone help me write it following what I wrong above?
It may have something to do within the MakeReq method call. I just tried the following code:
private void Form1_Load(object sender, EventArgs e)
{
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(listId.Items[a].ToString(), listCell.Items[b].ToString());
}
}
}
}
public void MakeReq(string listId, string cellId)
{
Console.WriteLine("store.domain.com/" + listId + "sec01&ire=" + cellId);
}
And it gave the output:
store.domain.com/ID_10sec01&ire=Cell_1
store.domain.com/ID_10sec01&ire=Cell_2
store.domain.com/ID_10sec01&ire=Cell_3
store.domain.com/ID_10sec01&ire=Cell_4
store.domain.com/ID_10sec01&ire=Cell_5
store.domain.com/ID_10sec01&ire=Cell_6
store.domain.com/ID_10sec01&ire=Cell_7
store.domain.com/ID_10sec01&ire=Cell_8
store.domain.com/ID_10sec01&ire=Cell_9
store.domain.com/ID_10sec01&ire=Cell_10
Isn't that what you wanted?
I think there might be some side-effects in MakeReq that you are not showing us. How about adding some debug statements to your code, like this:
if (listId.Items.Count != 0 && listCell.Items.Count != 0)
{
for (int a = 0; a < listId.Items.Count; a++)
{
int listCellCount = listCell.Items.Count;
for (int b = 0; b < listCell.Items.Count; b++)
{
lblID.Text = listId.Items[a].ToString();
MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() +
"&ire=1", listCell.Items[b].ToString());
//System.Threading.Thread.Sleep(5);
Debug.Assert(listCellCount == listCell.Items.Count);
}
}
}
The assertion (listCellCount == listCell.Items.Count) should always be true, unless something is changing listCell.Items.

Categories

Resources