如何将选定的行从GRIDVIEW插入表中 [英] How to Insert the Selected Row From GRIDVIEW into table

查看:74
本文介绍了如何将选定的行从GRIDVIEW插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个网格视图,一个连接到一个表,另一个连接到同一数据库中的其他表



table1

table2



gridview1

gridview2



当gridview1中有记录时使用gridview选择工具选择然后通过单击按钮它将记录移动到table2并现在显示在gridview 2

iv'e尝试



  protected   void  GridView1_SelectedIndexChanged(对象发​​件人,EventArgs e)
{
GridViewRow r = GridView1.SelectedRow;
string strQuery = 插入表值(' + r.Cells [ 0 ] + ',' + r.Cells [ 1 ] + < span class =code-string>');
}







但是我需要它连接到button1

解决方案

您可以将当前选定的行存储为类变量,然后在单击按钮时引用它



 public GridViewRow r = null; 

//行选择
r = GridView1.SelectedRow;

//按钮点击
如果(r!= null){/ *做某事* /}


< pre lang =c#> protected void btnSend_Click( object sender,EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add( MenuName);
dt.Columns.Add( MenuID);

foreach (GridViewRow grdRow gridview1.Rows) /// ///////////从GridMenuList添加新数据到Datatable DT
{
if (((CheckBox)grdRow.FindControl( chkSelect))。已检查)
{
DataRow drow = dt.NewRow();

标签lblMenuName =(标签)grdRow.FindControl( lblMenuName) ;

HiddenField hdnID =(HiddenField)grdRow.FindControl( hdnID) ;

drow [ MenuName] = lblMenuName.Text;
drow [ MenuID] = hdnID.Value;

dt.Rows.Add(卓尔);
}
}
如果(dt.Rows.Count > 0
{
gridview2.DataSource = dt;
gridview2.DataBind();
}
}

受保护 void gridview2_RowDataBound( object sender,GridViewRowEventArgs e)
{

if (e .Row.RowType == DataControlRowType.DataRow)
{
DataRowView dr =(DataRowView)e.Row.DataItem;

// e.Row.Cells [0] .Text = dr [MenuName ] .ToString();
标签lblMenuName = e.Row.FindControl( lblMenuName as 标签;
lblMenuName.Text = dr [ MenuName]。ToString();

HiddenField hdnID = e.Row.FindControl( hdnID)< span class =code-keyword> as HiddenField;
hdnID.Value = dr [ MenuID]。ToString();
}
}


i have two gridviews one is connected to one table and the the other is connected to other table both in the same database

table1
table2

gridview1
gridview2

when a record in gridview1 is selected using the gridview select tool then by clicking a button it will move the record to table2 and now shown on gridview 2
iv'e tried

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow r = GridView1.SelectedRow;
        string strQuery = "insert into table values('" + r.Cells[0] + "','" + r.Cells[1] + "')";
    }




but i need it connected to button1

解决方案

You could store the current selected row as a class variable, then reference it when button is clicked

public GridViewRow r = null;

//row seleccted
r = GridView1.SelectedRow;

//button clicked
if(r!=null){/*do something*/}


protected void btnSend_Click(object sender, EventArgs e)
{
        DataTable dt = new DataTable();
        dt.Columns.Add("MenuName");
        dt.Columns.Add("MenuID");

        foreach (GridViewRow grdRow in gridview1.Rows) ////////////// Add new Data from GridMenuList TO Datatable DT
            {
                if (((CheckBox)grdRow.FindControl("chkSelect")).Checked)
                {
                    DataRow drow = dt.NewRow();

                    Label lblMenuName = (Label)grdRow.FindControl("lblMenuName");

                    HiddenField hdnID = (HiddenField)grdRow.FindControl("hdnID");

                    drow["MenuName"] = lblMenuName.Text;               
                    drow["MenuID"] = hdnID.Value;
                    
                    dt.Rows.Add(drow);
                }
            }
        if (dt.Rows.Count > 0)
        {
            gridview2.DataSource = dt;
            gridview2.DataBind();
        }
}

protected void gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
{

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView dr = (DataRowView)e.Row.DataItem;
           
            //e.Row.Cells[0].Text = dr["MenuName"].ToString();
            Label lblMenuName = e.Row.FindControl("lblMenuName") as Label;
            lblMenuName.Text = dr["MenuName"].ToString();

            HiddenField hdnID = e.Row.FindControl("hdnID") as HiddenField;
            hdnID.Value = dr["MenuID"].ToString();
         }
}


这篇关于如何将选定的行从GRIDVIEW插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆