我无法在同一页面上更新第二个gridview中的数据 [英] I am not able to update data in 2nd gridview on same page

查看:51
本文介绍了我无法在同一页面上更新第二个gridview中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的HTML: -



 <  < span class =code-leadattribute> asp:GridView     ID   =  GridView2    runat   =  server    AutoGenerateColumns   =  False < span class =code-attribute> 

< span class =code-attribute> DataKeyNames = RefrenceNo onrowcancelingedit = GridView2_RowCancelingEdit

onrowediting = GridView2_RowEditing onrowupdating = GridView2_RowUpdating >
< >
< asp:TemplateField HeaderText = 图片 >
< ItemTemplate >
< img src =' Img / <% #Eval ( image%> ' width = 80px height = 80px / >
< / ItemTemplate >
< / asp:TemplateField >
< asp:TemplateField >
< ItemTemplate >
< asp:标签 ID = Label1 runat = server < span class =code-attribute> 文本 =' <% #Eval( detail%> ; ' > < / asp:标签 >
< / ItemTemplate >
< EditItemTemplate >
< < span class =code-leadattribute> asp:TextBox ID = text runat = server 文本 =' <% #Eval( detail%> ' > < / asp:TextBox >
< / EditItemTemplate >
< / asp:TemplateField >
< asp:CommandField ShowEditButton = True / >
< ; asp:CommandField ShowDeleteButton = True / >
< /列 >
< / asp:GridView >







这是我的代码: -





公共部分分类s editgall:System.Web.UI.Page

{

protected void Page_Load(object sender,EventArgs e)

{



if(Page.IsPostBack!= true)

{

bindgrid();

}



}



public void bindgrid()

{

GridView2.Visible = false;

string str = @data source = DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true;

SqlConnection con = new SqlConnection(str);

SqlDataAdapter da = new SqlDataAdapter(select dateddate from date1,con);

con.Open();

DataSet ds = new DataSet();

da.Fill(ds,date1);

GridView1.DataSource = ds;

GridView1.DataBind();

con.Close();





string str1 = @data source = DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true;

SqlConnection con1 = new SqlConnection(str1);

SqlDataAdapter da1 = new SqlDataAdapter(select image,detail from date1,con1);

con1.Open();

DataSet ds1 = new DataSet();

da1.Fill(ds1,date1);

GridView2.DataSource = ds1;

//GridView2.DataBind();

con1.Close();

}



protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)

{

string str = @data源= DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true;

SqlConnection con = new SqlConnection(str);

con.Open();

string s = GridView1。 DataKeys [e.RowIndex] .Value.ToString();



SqlCommand cmd = new SqlCommand(从date1删除,其中ddate ='+ s +' ,con);

cmd.ExecuteNonQuery();

con.Close();





Response.Write(已成功删除);

}







protected void GridView2_RowEditing(object sender,GridViewEditEventArgs e)

{

string str = @data source = DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true;

SqlConnection con = new SqlConnection(str);

SqlDataAdapter da = new SqlDataAdapter(Select * from date1,con);

con.Open();

DataSet ds = new DataSet();

da.Fill(ds,date1);

GridView2.EditIndex = e.NewEditIndex;

GridView2.DataSource = ds;

GridView2.DataBind();

con。关闭();

}

protected void GridView2_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)

{

string str = @数据源= DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true;

SqlConnection con = new SqlConnection(str);

SqlDataAdapter da = new SqlDataAdapter(Select * from date1,con);

con.Open();

DataSet ds = new DataSet();

da.Fill(ds,date1);

GridView2.EditIndex = -1;

GridView2.DataSource = ds;

GridView2.DataBind();

con.Close ();

}

protected void GridView2_RowUpdating(object sender,GridViewUpdateEventArgs e)

{

string str = @数据源= DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true;

SqlConnection con = new SqlConnection(str);

con.Open();

TextBox s =(TextBox )(GridView2.Rows [e.RowIndex] .FindControl(text));

string id = GridView2.DataKeys [e.RowIndex] .Value.ToString();



我想更新我的数据: -

< b> // Response.Write(update date1 set detail ='+ s.Text +'where refrenceNo ='+ id +');

// SqlCommand cmd = new SqlCommand(update date1 set detail ='+ s +',con);

//cmd.ExecuteNonQuery();

con。关闭();



bindgrid();

}





protected void Unnamed1_Click(object sender,EventArgs e)

{

GridView2.Visible = true;



LinkBut​​ton lnk =发送者为LinkBut​​ton;

TextBox1.Tex t = lnk.Text;

string str1 = @data source = DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true;

SqlConnection con1 = new SqlConnection(str1);

SqlDataAdapter da1 = new SqlDataAdapter(select * from date1 where ddate ='+ TextBox1。文字+',con1);

con1.Open();

DataSet ds1 = new DataSet();

da1.Fill( ds1,date1);

GridView2.DataSource = ds1;

GridView2.DataBind();



// LinkBut​​ton lnk =发送者为LinkBut​​ton;

//TextBox1.Text = lnk.Text;



con1.Close();

}

}

解决方案

问题是由于您的GridView2中添加了DataKeyNames = RefrenceNo但在您的选择查询中,您没有使用RefrenceNo字段来选择。


Gridview设计中的
更改DataKeyNames或在您的选择查询中包含RefrenceNo绑定Gridview2的地方。



检查你的代码



 <   asp:GridView     ID   =   GridView2    runat   =  server    AutoGenerateColumns   =  False  

< span class =code-attribute> DataKeyNames = RefrenceNo onrowcancelingedit = GridView2_RowCancelingEdit

< span class =code-attribute> onrowediting = GridView2_RowEditing onrowupdating = GridView2_RowUpdating >





  string  str1 =  @  data source = DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true; 
SqlConnection con1 = new SqlConnection(str1);
SqlDataAdapter da1 = new SqlDataAdapter( 选择图像,详细信息来自date1,con1) ;
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1, date1);
GridView2.DataSource = ds1;
// GridView2.DataBind();
con1.Close();





上面的代码应该是这样的



  string  str1 =  @  data source = DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; integrated security = true; 
SqlConnection con1 = new SqlConnection(str1);
SqlDataAdapter da1 = new SqlDataAdapter( 从date1选择图像,细节,RefrenceNo , con1);
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1,< span class =code-string> date1);
GridView2.DataSource = ds1;
// GridView2.DataBind();
con1.Close();


This is my html:-

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"

            DataKeyNames="RefrenceNo" onrowcancelingedit="GridView2_RowCancelingEdit"

            onrowediting="GridView2_RowEditing" onrowupdating="GridView2_RowUpdating">
        <Columns>
            <asp:TemplateField HeaderText="Pics">
                <ItemTemplate>
                    <img src='Img/<%#Eval("image") %>' width="80px" height="80px" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                   <asp:Label ID="Label1" runat="server" Text='<%#Eval("detail") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                   <asp:TextBox ID="text" runat="server" Text='<%#Eval("detail") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        </asp:GridView>




And this is my code:-


public partial class editgall : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (Page.IsPostBack != true)
{
bindgrid();
}

}

public void bindgrid()
{
GridView2.Visible = false;
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("Select distinct ddate from date1", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "date1");
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();


string str1 = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con1 = new SqlConnection(str1);
SqlDataAdapter da1 = new SqlDataAdapter("Select image,detail from date1", con1);
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1, "date1");
GridView2.DataSource = ds1;
//GridView2.DataBind();
con1.Close();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
con.Open();
string s = GridView1.DataKeys[e.RowIndex].Value.ToString();

SqlCommand cmd = new SqlCommand("Delete from date1 where ddate='" + s + "'",con);
cmd.ExecuteNonQuery();
con.Close();


Response.Write("Deleted Sucessfully");
}



protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("Select * from date1", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "date1");
GridView2.EditIndex = e.NewEditIndex;
GridView2.DataSource = ds;
GridView2.DataBind();
con.Close();
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("Select * from date1", con);
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "date1");
GridView2.EditIndex = -1;
GridView2.DataSource = ds;
GridView2.DataBind();
con.Close();
}
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con = new SqlConnection(str);
con.Open();
TextBox s = (TextBox)(GridView2.Rows[e.RowIndex].FindControl("text"));
string id = GridView2.DataKeys[e.RowIndex].Value.ToString();

Here I want to UPDATE my data:-
// Response.Write("update date1 set detail='" + s.Text + "'where RefrenceNo='" + id + "'");
//SqlCommand cmd = new SqlCommand("update date1 set detail='" + s + "'", con);
//cmd.ExecuteNonQuery();

con.Close();

bindgrid();
}


protected void Unnamed1_Click(object sender, EventArgs e)
{
GridView2.Visible = true;

LinkButton lnk = sender as LinkButton;
TextBox1.Text = lnk.Text;
string str1 = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con1 = new SqlConnection(str1);
SqlDataAdapter da1 = new SqlDataAdapter("Select * from date1 where ddate='"+TextBox1.Text+"'", con1);
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1, "date1");
GridView2.DataSource = ds1;
GridView2.DataBind();

//LinkButton lnk = sender as LinkButton;
//TextBox1.Text = lnk.Text;

con1.Close();
}
}

解决方案

The problem is due to in your GridView2 you have added the DataKeyNames="RefrenceNo" but in your select query you didnt use the "RefrenceNo" field to select .

in Gridview design change the DataKeyNames or include the "RefrenceNo" in your select query where you bind your Gridview2.

check your code

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"

            DataKeyNames="RefrenceNo" onrowcancelingedit="GridView2_RowCancelingEdit"

            onrowediting="GridView2_RowEditing" onrowupdating="GridView2_RowUpdating">



string str1 = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con1 = new SqlConnection(str1);
SqlDataAdapter da1 = new SqlDataAdapter("Select image,detail from date1", con1);
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1, "date1");
GridView2.DataSource = ds1;
//GridView2.DataBind();
con1.Close();



The above code should be like this

string str1 = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
SqlConnection con1 = new SqlConnection(str1);
SqlDataAdapter da1 = new SqlDataAdapter("Select image,detail,RefrenceNo from date1", con1);
con1.Open();
DataSet ds1 = new DataSet();
da1.Fill(ds1, "date1");
GridView2.DataSource = ds1;
//GridView2.DataBind();
con1.Close();


这篇关于我无法在同一页面上更新第二个gridview中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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