For Programmers: Free Programming Magazines  


Home > Archive > C# > January 2005 > How to create a DataGrid with check boxes generically









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to create a DataGrid with check boxes generically
peteshaw

2005-01-15, 3:57 am

I have been trying to find a simple answer this problem for a few days
now, and was wondering if anyone had access to a simple answer.

I just want to creat a web page with a datagrid, with a text box above
it in which you can enter an SQL string. Once you enter the SQL,
"select * from employees", and press submit, the datagrid is bound, and
the columns are automatically generated.

Once the grid is generated, how can I add a column of check boxes, to
allow the user to select rows to delete, edit, etc..? Its a simple
enough problem if you only have one table you are binding to, but if
the table is going to vary, I don't know how to proceed.
Thanks for listening, best to all of you.

--Pete

minmin

2005-01-18, 3:57 am

Dear peteshaw,
Here is the simple DataGrid with checkboxes example that can help you.

Let's suppose that we have the DataGrid called DataGrid1 and the
DataSet called DataSet1 which contains the data getting from the SQL
String.

At first,
you should create the checkbox column class .ie.,
public class CheckBoxColumn : DataGridColumn
{

public CheckBoxColumn(): base()
{

}

public override void InitializeCell(TableCell cell,
int columnIndex, ListItemType itemType)
{


if( itemType == ListItemType.EditItem ||
itemType == ListItemType.Item ||
itemType == ListItemType.AlternatingItem ||
itemType == ListItemType.SelectedItem)
{

CheckBox checkbox = new CheckBox();
checkbox.ID = "checkboxDelete";
cell.Controls.Add(checkbox);
}
}
}

After that ,please write this
CheckBoxColumn checkCol = new CheckBoxColumn();
DataGrid1.Columns.Add(checkCol);
Before
DataView Source = DataSet1.Tables[0].DefaultView;
DataGrid1.DataSource = Source;
DataGrid1.DataBind();

And if you want to get the checkbox checked rows ,
DataGridItemCollection items = DataGrid1.Items;
for (int i=0; i < DataGrid1.Items.Count ; i++)
{
CheckBox chkBox = (CheckBox)
items[i].Cells[0].FindControl("checkboxDelete");
if ( chkBox != null && chkBox.Checked )
{
//Do something here
}
}

I hope that it will help you.Please let me know if you want to ask
about it again.

yours,
Tin Zaw Min(minmin)

peteshaw wrote:
> I have been trying to find a simple answer this problem for a few

days
> now, and was wondering if anyone had access to a simple answer.
>
> I just want to creat a web page with a datagrid, with a text box

above
> it in which you can enter an SQL string. Once you enter the SQL,
> "select * from employees", and press submit, the datagrid is bound,

and
> the columns are automatically generated.
>
> Once the grid is generated, how can I add a column of check boxes, to
> allow the user to select rows to delete, edit, etc..? Its a simple
> enough problem if you only have one table you are binding to, but if
> the table is going to vary, I don't know how to proceed.
> Thanks for listening, best to all of you.
>
> --Pete


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com