English 中文(简体)
数据网格 请帮助我解决问题。
原标题:datagridview logical- Please help me to fix my problem

I m tired to fix my logical error in my datagridview, can someone please help me. Here my code.

......

while (dr.Read())
            {

                int id = dr.GetInt32(0);
                DateTime startBook = dr.GetDateTime(2);
                DateTime endBook = dr.GetDateTime(3);

                int startBook_C = startBook.Day;
                int endBook_C = endBook.Day;

                int TotalDays = endBook_C - startBook_C;

                //dataGridView1(startBook_C, id - 1).Style.BackColor = Color.Blue;
                dataGridView1.Rows[startBook_C].Cells[endBook_C-1].Style.BackColor = Color.Blue;
                int i;
                for (i = 1; i <= TotalDays; i++)
                {
                    dataGridView1.Rows[startBook_C + i].Cells[endBook_C -1].Style.BackColor = Color.Blue;
                    //dataGridView1.Rows[startBook_C].Cells[carID - 1].Style.BackColor = Color.Blue;
                    // dataGridView1(startBook_C + i, id - 1).Style.BackColor = Color.Red;/* TODO ERROR: Skipped SkippedTokensTrivia */
                    TotalDays -= 1;
                }



            }
            Con.Close();

......

我对数据网格的看法有问题,它有逻辑错误,我不知道我的错误是什么,为什么我在这里请各位主注意帮助确定我在数据网概览中的逻辑错误。 我的图像如下,请访问链接,看我的含义。

“在此处的影像描述”

https://i.sstatic.net/AeUyrZ8J.png” rel=“nofollow noreferer”>“entergraph

问题回答

Consider startBook.Day= day 1 and endBook.Day = day 3 ( the row of ID 23). And the first loop i =1;

Then dataGridView1.Rows[startBook_C + i].Cells[endBook_C -1] is dataGridView1.Rows[**2**].Cells[**2**](start from 0) so the image provided shows DataGrid view table[3][3] is color blue .

在ID 23中,浏览量为0(第一行)。 电池从4台开始(第5台,被抵消为4台/ID+CARID+bookstartDay+book EndDay=4)。

总工资——=1;休息时间不需要。

while (dr.Read())
            {
                int offset = 4;//ID+CARID+book StartDay+book EndDay=4.
                int id = dr.GetInt32(0);
                DateTime startBook = dr.GetDateTime(2);
                DateTime endBook = dr.GetDateTime(3);

                int startBook_C = startBook.Day;//1
                int endBook_C = endBook.Day;//3

                int TotalDays = endBook_C - startBook_C; //2
               
                for (int i = 0; i <= TotalDays; i++)
                {
                   dataGridView1.Rows[0].Cells[offset+startBook_C - 1+i].Style.BackColor = Color.Blue;
                  
                }

            }
            Con.Close();

最新情况:由于实例中的许多方法没有提供明确的定义,我已自行提供了方法。

   int offset = 4;
   foreach (DataGridViewRow row in dataGridView1.Rows)
   {

       if (!row.IsNewRow)
       {

           if (row.Cells.Count >= 4)
           {

               DateTime startBook;
               DateTime endBook;
               if (DateTime.TryParse(row.Cells[2].Value?.ToString(), out startBook) &&
                   DateTime.TryParse(row.Cells[3].Value?.ToString(), out endBook))
               {

                   int startBook_C = startBook.Day;
                   int endBook_C = endBook.Day;
                   int TotalDays = endBook_C - startBook_C;
                   for (int i = 0; i <= TotalDays; i++)
                   {
                       dataGridView1.Rows[row.Index].Cells[offset + startBook_C - 1 + i].Style.BackColor = Color.Blue;

                   }
               }
           }
       }
   }

“entergraph





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签