开发者

Copy all data from one gridview to another, but those from the first grid which are ticked

I have two grids on the same page. The first grid contains data and has check boxes enabled. Upon the click of a button, I want to copy only the data that is ticked to the other grid.

I have done this ­— but it only copies one item from the first grid to second. Please help.

My code:

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;Integrat开发者_运维问答ed 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();
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜