我怎样才能删除网格上的按钮单击事件选择的数据? [英] How can i delete selected data from grid on the button click event?
本文介绍了我怎样才能删除网格上的按钮单击事件选择的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有我的网页上一个网格,并使用复选框选中一个或电网多行,我想删除删除按钮的点击是出方电网,从电网检查排..
在表中我只有名称字段有一个像身份证号码字段。
我怎样才能删除记录。
在此先感谢..
下面是我的code我在做什么: -
私人无效的GetData()
{ ArrayList的ARR;
如果(的ViewState [总记录]!= NULL)
{
ARR =(ArrayList中)的ViewState [总记录];
}
其他
{
ARR =新的ArrayList();
}
的for(int i = 0; I< grdlistWord.Rows.Count;我++)
{ 复选框CHK =(复选框)grdlistWord.Rows [I] .Cells [1] .FindControl(chkWord);
如果(chk.Checked)
{
如果(!arr.Contains(grdlistWord.Rows [I] .Cells [1]))
{
arr.Add(grdlistWord.Rows [I] .Cells [1]);
}
}
其他
{
如果(arr.Contains(grdlistWord.Rows [I] .Cells [1]))
{
arr.Remove(grdlistWord.Rows [I] .Cells [1]);
}
} }
的ViewState [总记录] = ARR;
}保护无效lnkbtnDelete_Click(对象发件人,EventArgs的发送)
{ 尝试
{
诠释计数= 0; ArrayList的ARR =(ArrayList中)的ViewState [总记录];
数= arr.Count;
的for(int i = 0; I< grdlistWord.Rows.Count;我++)
{
如果(arr.Contains(grdlistWord.Rows [I] .Cells [1]。文本))
{
回复于(grdlistWord.Rows [I] .Cells [1] .Text.ToString());
DeleteRecord(grdlistWord.Rows [I] .Cells [1] .Text.ToString());
arr.Remove(grdlistWord.Rows [I] .Cells [1]。文本);
}
}
的ViewState [总记录] = ARR;
GridBind(); }
赶上(SQLEXCEPTION EX)
{
ex.ToString();
}}
私人无效DeleteRecord(串词)
{ 查询字符串=删除搜索内容,其中字= @词; CMD的SqlCommand =新的SqlCommand(查询,CON);
cmd.Parameters.AddWithValue(@字字);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
GridView控件的HTML细节:
<&字段集GT;
<传奇>列表与LT; /传说>
< ASP:GridView控件ID =grdlistWord=服务器的DataKeyNames =字的AutoGenerateColumns =FALSE的cellpadding =4前景色=#333333网格=无AllowPaging =真OnRowDataBound =grdlistWord_RowDataBound OnRowDeleting =grdlistWord_RowDeleting>
<柱体和GT;
< ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:复选框ID =chkWord=服务器的onclick =check_click(本); /> < / ItemTemplate中>
<&HeaderTemplate中GT;
< ASP:复选框ID =chkAll=服务器的onclick =checkAll(本); />
< ASP:LinkButton的ID =ButtonDelete=服务器文本=删除的OnClick =ButtonDelete_Click>< / ASP:LinkButton的>
< / HeaderTemplate中>
< / ASP:的TemplateField>
< ASP:BoundField的数据字段=字的HeaderText =字/>
< ASP:HyperLinkField字段的HeaderText =编辑文本=编辑DataNavigateUrlFields =字DataNavigateUrlFormatString =?SearchWord.aspx字= {0}&放大器;放大器;模式=编辑/>
< ASP:CommandField中的ShowDeleteButton =真的HeaderText =删除/>
< /专栏>
< FooterStyle背景色=#5D7B9DFONT-粗体=真前景色=白/>
< RowStyle的BackColor =#F7F6F3前景色=#333333/>
< EditRowStyle背景色=#999999/>
< SelectedRowStyle背景色=#E2DED6FONT-粗体=真前景色=#333333/>
< PagerStyle背景色=#284775前景色=白HorizontalAlign =中心/>
< HeaderStyle背景色=#5D7B9DFONT-粗体=真前景色=白/>
< AlternatingRowStyle背景色=白前景色=#284775/>
< EmptyDataTemplate>记录不存在<!/ EmptyDataTemplate>
< / ASP:GridView的> < ASP:HiddenField ID =hfCount=服务器VALUE =0/> < /字段集>
解决方案
保护无效lnkbtnDelete_Click(对象发件人,EventArgs的发送)
{ INT计数器= 0;
清单<串GT;字=新的List<串GT;();
的foreach(在grdlistWord.Rows GridViewRow rowitem)
{
如果(((复选框)rowitem.Cells [0] .FindControl(chkWord))==选中真。)//我认为复选框在第一列的索引---> 0
{
反++;
字词。(rowitem.Cells [1]。文本); //我认为这个词是在第二列索引---> 1
}
}
////////////////////////////////////////////////// ///////////
如果(反== 0)//没有检查
{ //显示一些消息框澄清,没有行已被选中。 }
////////////////////////////////////////////////// ///////////
如果(反== 1)//一张支票
{ DeleteRecord(字[0]);
//显示一些消息框,以澄清该操作已成功执行。 }
////////////////////////////////////////////////// ///////////
如果(计数器→1)//多个检查
{
的for(int i = 0; I< words.Count;我++)
{
DeleteRecord(字由[i]);
}
//显示一些消息框,以澄清该操作已成功执行。
} grdlistWord.DataBind();
}
I have a grid on my page and using check box for checking one or more than one row in grid and i want to delete checked row from grid on the click of delete button which is out side the grid..
In table i have only name field there is no field like ID.
How can i delete record.
Thanks in advance..
Here is my code What i am doing:-
private void GetData()
{
ArrayList arr;
if (ViewState["TotalRecords"] != null)
{
arr = (ArrayList)ViewState["TotalRecords"];
}
else
{
arr = new ArrayList();
}
for (int i = 0; i < grdlistWord.Rows.Count; i++)
{
CheckBox chk = (CheckBox)grdlistWord.Rows[i].Cells[1].FindControl("chkWord");
if (chk.Checked)
{
if (!arr.Contains(grdlistWord.Rows[i].Cells[1]))
{
arr.Add(grdlistWord.Rows[i].Cells[1]);
}
}
else
{
if (arr.Contains(grdlistWord.Rows[i].Cells[1]))
{
arr.Remove(grdlistWord.Rows[i].Cells[1]);
}
}
}
ViewState["TotalRecords"] = arr;
}
protected void lnkbtnDelete_Click(object sender, EventArgs e)
{
try
{
int count = 0;
ArrayList arr = (ArrayList)ViewState["TotalRecords"];
count = arr.Count;
for (int i = 0; i < grdlistWord.Rows.Count; i++)
{
if (arr.Contains(grdlistWord.Rows[i].Cells[1].Text))
{
Response.Write(grdlistWord.Rows[i].Cells[1].Text.ToString());
DeleteRecord(grdlistWord.Rows[i].Cells[1].Text.ToString());
arr.Remove(grdlistWord.Rows[i].Cells[1].Text);
}
}
ViewState["TotalRecords"] = arr;
GridBind();
}
catch (SqlException ex)
{
ex.ToString();
}
}
private void DeleteRecord(string word)
{
string query = "delete from searchword where word=@word";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@word", word);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
gridview html detail:
<fieldset>
<legend>List</legend>
<asp:GridView ID="grdlistWord" runat="server" DataKeyNames="word" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" OnRowDataBound="grdlistWord_RowDataBound" OnRowDeleting="grdlistWord_RowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkWord" runat="server" onclick="check_click(this);" />
</ItemTemplate>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" onclick="checkAll(this);" />
<asp:LinkButton ID="ButtonDelete" runat="server" Text="Delete" OnClick="ButtonDelete_Click"></asp:LinkButton>
</HeaderTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Word" HeaderText="Word" />
<asp:HyperLinkField HeaderText="Edit" Text="edit" DataNavigateUrlFields="Word" DataNavigateUrlFormatString="SearchWord.aspx?words={0}&mode=Edit" />
<asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EmptyDataTemplate>Records not exist!</EmptyDataTemplate>
</asp:GridView>
<asp:HiddenField ID="hfCount" runat="server" Value = "0" />
</fieldset>
解决方案
protected void lnkbtnDelete_Click(object sender, EventArgs e)
{
int counter = 0;
List<string> words = new List<string>();
foreach (GridViewRow rowitem in grdlistWord.Rows)
{
if (((CheckBox)rowitem.Cells[0].FindControl("chkWord")).Checked == true)//i consider that the check box is in the first column index ---> 0
{
counter++;
words.Add(rowitem.Cells[1].Text); //i consider that the word is in the second column index ---> 1
}
}
/////////////////////////////////////////////////////////////
if(counter == 0) //no checks
{
//show some message box to clarify that no row has been selected.
}
/////////////////////////////////////////////////////////////
if (counter == 1) //one check
{
DeleteRecord(words[0]);
//Show some message box to clarify that the operation has been executed successfully.
}
/////////////////////////////////////////////////////////////
if (counter > 1) //more than one check
{
for(int i=0; i<words.Count;i++)
{
DeleteRecord(words[i]);
}
//Show some message box to clarify that the operation has been executed successfully.
}
grdlistWord.DataBind();
}
这篇关于我怎样才能删除网格上的按钮单击事件选择的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文