tags:

views:

8

answers:

1

I have 2 girds on same page.

1st grid contains data and have check boxes enabled

i have to copy that data to other gird which is ticked.. on click of a insert button

i have done this ..but it only copies 1 item from gird 1 to second plz help

code done by me

int RowNo = 0;
foreach (GridViewRow row in GridView1.Rows) { RowNo = RowNo + 1;

        bool Checkbox = ((CheckBox)row.FindControl("CheckBox1")).Checked;


            if (Checkbox = true)
            {
                SqlConnection myConn = new SqlConnection();
                myConn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\eRashshala.mdf;Integrated Security=True;User Instance=True";
                myConn.Open();          

                GridViewRow Row = GridView1.Rows[RowNo - 1];
                String Name = Row.Cells[2].Text;                   
                SqlDataAdapter da = new SqlDataAdapter("Select LatinName,IngName,MaterialForm from IngredientInfo Where IngName='" + Name + "'", myConn);
                DataSet ds = new DataSet();
                da.Fill(ds, "IngredientInfo");
                GridView2.DataSource = ds.Tables[0];
                GridView2.DataBind();


            }
A: 

It looks to me as though you are only performing the copy for a single row in your grid, which would explain why only one thing is being copied. You start with:

bool Checkbox = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if (Checkbox = true)
    {
        // Perform copy...
    }

But as far as I can see, the first line applies only to the current ROW of data. Presumably the variable "row" is acquired in some way from the Grid. Is the code you show part of a foreach loop?

If not, then I imagine all you need to do is loop all the rows in your Grid, and perform the insertion for each row, rather than just one. You may (and probably should) group your writes together into a single DB transaction, rather than writing multiple times, but this is a performance optimisation and doesn't affect the functionality.

xan