Help with this

M

momo

Hello all,

I need help with this. I have a datagrid (dg1) with checkboxes and I want
when some rows are selected to populate another datagrid (dg2) with the
selected rows from (dg1).

Thanks

Momo
 
A

Alvin Bruney

What you need to do here is (or at least one approach) to retrieve the
checkboxes from the datagrid. Here it depends on how you added them and how
you designed your system. By far the easiest solution to implement depends
on whether or not you have a primary key set up in your database. If you do,
then you
 
A

Alvin Bruney

Oops, wasnt done yet. If your database has a primary key, then you can use
the datakeys object to loop thru and pick out your values of the row to
which the check box is attached. You will find the check box normally from
the request.forms object. this is totally dependent on how you added the
check boxes in the first place. The datakeys option results in much less
code than any other option i can think of.

Another option is to loop thru the grid looking for a checked check box.
You'd use the datagrid.items object to cycle thru in the loop construct
retrieving the controls. For each control you find, you will cast to a check
box object and then examine the checked property. If it is checked, you loop
thru each cell of the cells object to retrieve the data. you will use that
stored source to populate the new datagrid.

There is another option. Use your first datagrid to find the index of the
checked row, then go find that row in the underlying dataset. add that row
to the dataset2 which forms the datasource of datagrid2. Then rebind. You
need to add a new row, you can't just add a reference because a row must
exist in exactly one dataset. If i've rambled on and on, ask me to clarify.
or somebody else can.
 
M

momo

Alvin,

Will you please show me the code on how to do solution two and three.

Thanks,

Momo
 
A

Alvin Bruney

This code retrieves checked values of radiolist buttons embedded in the
first column of a datagrid.
Modify the following disparate snippets to suit your needs. They weren't all
taken from the same application so it would need some minor modification,
but the logic is what is important.

//the radio buttons were added this way in the itemcreated event handler
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)

{

//column 1 is an itemtemplate column

Label lbl = (Label)e.Item.FindControl("Dig");

if(lbl != null)

lbl.Text = "<input type=radio name='samegroup' value=" +
e.Item.Cells[1].Text + ">";

}

//use this to retrieve the selected radio button in the entire grid thru the
form object

private string GetSelectedItems()

{

//individual radiobuttons are tied together using samegroup

string retval = Request.Form["samegroup"];

if(retval == null || String.Empty == retval)

Page.Controls.Add(new LiteralControl("<script>alert('Please select a row to
dig on by enabling a radio button in the grid')</script>"));

return retval;

}



//this code retrieves the row for the selected radio button

ArrayList currentRow = new ArrayList();

for(int i = 0; i < e.Item.Cells.Count; i++)

currentRow.Add(e.Item.Cells.Text);

ViewState["CURRENTROW"] = currentRow;

//this code adds a new row to a dataset
DataRow myRow = dsTemp.Tables[0].NewRow();

//pull the values out of session current row and add to a new row then
bind

myRow[loopcount]= value;

myRow[count + 2]= value2

dsTemp.Tables[0].Rows.Add(myRow);

//bind the result to the grid so that the new row can show up
dg2.DataSource = dsTemp;
dg2.DataBind();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top