Is there any way to show data from access table to some particular columns. For eg. Table have following data BCode TCode1 Slot1 TCode2 Slot2 TCode3 Slot Batch1 T1 10:00 T2 12:00
Batch2 T1 08:00 T2 09:00 T3 11:30 Batch3 T1 08:00 T2 10:00 T3 11:00

I want to show T1 under 16:00 T2 under 18:00 and t3 under 20:00 Means output should be some thing like this
f43af074ff01be0047da940ee3201603
I tried many ways but not reaching to the output.

Dani AI

Generated

Following 's sample data and 's pointer about addressing cells directly, a more robust and faster approach is to map column names to indices once, add the DataGridView row first, then set cells by direct index (with bounds checks). The common causes of the "Index was out of range" error in this thread were attempting to write into a row that didn't exist yet and scanning every column for every slot (inefficient and fragile when formats differ).

A concise VB.NET pattern that avoids nested full-column loops, normalizes time labels, and repeats a value for the next two slots:

' build name->index map
Dim colIndex As New Dictionary(Of String, Integer)()
For i As Integer = 0 To dataGridView1.ColumnCount - 1
    colIndex(dataGridView1.Columns(i).Name) = i
Next

For r As Integer = 0 To ds.Tables(0).Rows.Count - 1
    Dim src = ds.Tables(0).Rows(r)
    Dim newRowIndex As Integer = dataGridView1.Rows.Add(src("Batch").ToString())
    Dim slotPtr As Integer = 5    ' adjust to first Slot column in source
    Dim teacherPtr As Integer = 4 ' adjust to first Teacher column in source
    Dim slotsCount As Integer = Convert.ToInt32(src("p_count"))

    For s As Integer = 1 To slotsCount
        Dim rawTime = src(slotPtr).ToString().Trim()
        Dim norm = rawTime
        Dim dt As DateTime
        If DateTime.TryParse(rawTime, dt) Then norm = dt.ToString("HH:mm")

        If colIndex.ContainsKey(norm) Then
            Dim ci = colIndex(norm)
            dataGridView1.Rows(newRowIndex).Cells(ci).Value = src(teacherPtr).ToString()
            For rep As Integer = 1 To 2
                If ci + rep < dataGridView1.ColumnCount Then
                    dataGridView1.Rows(newRowIndex).Cells(ci + rep).Value = src(teacherPtr).ToString()
                End If
            Next
        End If

        slotPtr += 2
        teacherPtr += 2
    Next
Next

Notes and follow-up points: DataGridView does not support true cell‑spans — setting adjacent cells is the usual workaround; for visual merging, custom painting is required. For larger data sets, pivoting the source into a DataTable with one column per time slot and binding that table to the grid will be far faster than cell-by-cell writes. Ensure time strings are normalized (DateTime.TryParse/TryParseExact) so column-name matching is reliable.

Recommended Answers

All 6 Replies

You can adress the cells of a DGV somewhat like, you can do it in Excel.
Use something like myDGV.Rows[i].Cells[j].Value = myValue.ToString(); i and j are zero based.

I wrote this code and getting this output
1f5347686296adc9b35308daf3926592

And code is

            aAdapter1.Fill(ds1, "app_info");
            int recCount = ds1.Tables[0].Rows.Count;
            dataGridView1.ColumnCount = 27;
            dataGridView1.Columns[0].Name = "Batch";
            dataGridView1.Columns[1].Name = "08:00";
            dataGridView1.Columns[2].Name = "08:30";
            dataGridView1.Columns[3].Name = "09:00";
            dataGridView1.Columns[4].Name = "09:30";
            dataGridView1.Columns[5].Name ="10:00";
            dataGridView1.Columns[6].Name ="10:30";
            dataGridView1.Columns[7].Name ="11:00";
            dataGridView1.Columns[8].Name ="11:30";
            dataGridView1.Columns[9].Name ="12:00";
            dataGridView1.Columns[10].Name ="12:30";
            dataGridView1.Columns[11].Name ="13:00";
            dataGridView1.Columns[12].Name ="13:30";
            dataGridView1.Columns[13].Name ="14:00";
            dataGridView1.Columns[14].Name ="14:30";            
            for (int k = 0; k < recCount; k++)
            {
                int p_count = Convert.ToInt32(ds1.Tables[0].Rows[k][2].ToString());
                int col_count = 5;
                int teat = 4;
                string[] str = new string[p_count];
                int i = 0;
                string batchname = ds1.Tables[0].Rows[k][0].ToString();
                Console.Write(batchname+"   ");
                for (int x = 1; x <= p_count; x++)
                {
                    i++;
                    for (int m = 0; m < dataGridView1.ColumnCount; m++)
                    {

                        string colname = dataGridView1.Columns[m].Name;

                        string tablecoltime = ds1.Tables[0].Rows[k][col_count].ToString();
                        string teacher = ds1.Tables[0].Rows[k][teat].ToString();
                        if (colname == tablecoltime)
                        {
                            dataGridView1.Rows.Add(batchname, teacher, teacher, teacher, teacher, teacher, teacher, teacher, teacher, teacher, teacher, teacher, teacher);
                        }
                    }
                    teat += 2; col_count += 2;
                }
                Console.WriteLine();
            }
            dataGridView1.Columns[0].ReadOnly = true;

Because you seem have a Table from a DataSet, why not try this?

Thank you for suggestion
I used your suggested code but it is throwing expection Index was out of range.
Here is the modified part of the above code -

    if (colname == tablecoltime)
       {
            teacher[m] = ds1.Tables[0].Rows[k][teat].ToString();
            Console.WriteLine(teacher[m]);
            MessageBox.Show(k.ToString());
            dataGridView1.Rows[k].Cells[3].Value = ds1.Tables[0].Rows[k][teat].ToString();
       }
       else
       {
            teacher[m] = "";
       }

Yes I made some more modification in code and got the output what I was looking for here is the modified version of the code

for (int k = 0; k < recCount; k++)
{
    int p_count = Convert.ToInt32(ds1.Tables[0].Rows[k][2].ToString());
    int col_count = 5;
    int teat = 4;
    string[] str = new string[p_count];
    int i = 0;
    string batchname = ds1.Tables[0].Rows[k][0].ToString();
    string[] teacher=new string[dataGridView1.ColumnCount];
    dataGridView1.Rows.Add(batchname);
    for (int x = 1; x <= p_count; x++)
    {
        for (int m = 1; m < dataGridView1.ColumnCount; m++)
        {
            string colname = dataGridView1.Columns[m].Name;
            string tablecoltime = ds1.Tables[0].Rows[k][col_count].ToString();
            if (colname == tablecoltime)
            {
                teacher[m] = ds1.Tables[0].Rows[k][teat].ToString();
                dataGridView1.Rows[k].Cells[m].Value = ds1.Tables[0].Rows[k][teat].ToString();
            }
            else
            {
                teacher[m] = "";
            }
        }
        teat += 2; col_count += 2;
    }
}
dataGridView1.Columns[0].ReadOnly = true;

Well I know it is not allowed to ask futhuer query if current query is solved, but it is just a doubt if anyone clear it. How can I repeat same value for next 2 cells of the row. Say if P1 is under column 08:00 for B1 row how can I show P1 for column 08:30 and 09:00
Tankyou everybody in advance

Example, or use a for loop:
dataGridView1.Rows[k].Cells[m].Value="P1";
dataGridView1.Rows[k].Cells[m+1].Value="P1";
dataGridView1.Rows[k].Cells[m+2].Value="P1";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.