Click here to Skip to main content
15,041,393 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I will have my data grid view as follows

C#
RecordTypeCode   Content
   FileHeader      1111111111111
   BatchHeader     5666666666666
   EntryDetail     656546545644545
   BatchControl    8654654564564
   FileControl     945645

I would like to have check boxes only at BatchHeader and EntryDetail is it possible. This is the way i am binding data to data grid view

C#
if (line.StartsWith("1"))
{
  dcID = new DataColumn("RecordTypeCode");
  dt.Columns.Add(dcID);
  DataColumn dcSomeText = new DataColumn("Content");
  dt.Columns.Add(dcSomeText);
  dr = dt.NewRow();
  dr["RecordTypeCode"] = filecontrolvariables.rectype[line.Substring(0, 1)].ToString();
  dr["Content"] = line;
  dt.Rows.Add(dr);
}

if (line.StartsWith("5"))
{
   dr = dt.NewRow();
   dr = dt.NewRow();
  dr["RecordTypeCode"] = filecontrolvariables.rectype[line.Substring(0, 1)].ToString();
  dr["Content"] = line;
 dt.Rows.Add(dr);
}


I would like to add check boxes as per needed can any one help me I tried this in different ways

if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
  if (dataGridView2.Rows[e.RowIndex].Cells["RecordTypeCode"].Value.ToString() == "BatchHeader")
 {
    e.PaintBackground(e.ClipBounds, true);
     e.Handled = true;
  }
 }

 int columnIndex = -1;
 int rowIndex = -1;
 if (dataGridView2.CurrentCell != null)
 {
     columnIndex = dataGridView2.CurrentCell.ColumnIndex;
     rowIndex = dataGridView2.CurrentCell.RowIndex;
  }
   if (columnIndex == 0)
   {
     if (e.RowIndex >= 0)
    {
    if (dataGridView2.Rows[e.RowIndex].Cells["RecordTypeCode"].Value.ToString() == "Batch Header")
    {
      e.PaintBackground(e.ClipBounds, true);
      e.Handled = true;
     }
     }
   }

But my corresponding row is showing empty when i check it only for BatchHeader
Posted
Updated 26-Oct-11 21:59pm
v2

1 solution

Here is the solution

C#
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                //if (e.ColumnIndex == <index of="" your="" checkbox="" column=""> && (e.RowIndex == 1 || e.RowIndex == 2))
                if (dataGridView2.Rows[e.RowIndex].Cells["RecordTypeCode"].Value.ToString() == "Batch Header" && e.ColumnIndex == <index of="" your="" checkbox="" column="" should="" be="" 0="" sicne="" its="" the="" first="">)
                {
                    e.PaintBackground(e.ClipBounds, true);
                    e.Handled = true;
                }
            }
        }</index></index>
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900