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.

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?

@ddanbe 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.