0
刪除一行在GridView控件與DropDownList中這是我設計的網頁如何在ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="newsamplecomb._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged1"
Height="246px" onrowdatabound="GridView1_RowDataBound" Width="1017px">
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<Columns>
<asp:BoundField />
<asp:TemplateField>
<HeaderTemplate>
Rollno<br />
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" Height="23px" Width="73px"
AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged" >
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="155px" ReadOnly="True"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
English<br />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Height="17px" Width="103px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Tamil<br />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Height="16px" Width="103px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Maths
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Height="16px" Width="103px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Science
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Height="16px" Width="103px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Total<br />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Height="16px" Width="103px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Grade
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Height="16px" Width="103px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonAdd" runat="server" OnClick="ButtonAdd_Click1" Text="+" />
<asp:Button ID="Buttondel" runat="server" Text="Del"></asp:Button>
<asp:RequiredFieldValidator ID="rfv" runat="server" ErrorMessage="" ForeColor="White" ControlToValidate="TextBox1" Width="10" Display="None"></asp:RequiredFieldValidator>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
<%-- //<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ForeColor="White" ControlToValidate="TextBox1" Width="10"></asp:RequiredFieldValidator>--%>
這是我的CS代碼
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var ddl = (DropDownList)e.Row.FindControl("DropDownList1");
string connection_string = ConfigurationManager.ConnectionStrings["DBC"].ConnectionString;
SqlConnection con = new SqlConnection(connection_string);
con.Open();
SqlCommand cmd = new SqlCommand("Select * from stud_table", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
// con.Close();
ddl.DataSource = ds;
ddl.DataTextField = "Rollno";
ddl.DataValueField = "Rollno";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
dpditemdesc(sender,e);
DropDownList box1 = (DropDownList)GridView1.Rows[x].Cells[1].FindControl("DropDownList1");
if (box1.Text != "")
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[2].Enabled = false;
GridView1.Rows[i].Cells[3].Enabled = false;
GridView1.Rows[i].Cells[4].Enabled = false;
GridView1.Rows[i].Cells[5].Enabled = false;
GridView1.Rows[i].Cells[6].Enabled = false;
GridView1.Rows[i].Cells[7].Enabled = false;
GridView1.Rows[i].Cells[8].Enabled = false;
}
}
else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[2].Enabled = true;
GridView1.Rows[i].Cells[3].Enabled = true;
GridView1.Rows[i].Cells[4].Enabled = true;
GridView1.Rows[i].Cells[5].Enabled = true;
GridView1.Rows[i].Cells[6].Enabled = true;
GridView1.Rows[i].Cells[7].Enabled = true;
GridView1.Rows[i].Cells[8].Enabled = true;
}
}
}
public void dpditemdesc(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
x = row.RowIndex;
TextBox box = (TextBox)GridView1.Rows[x].Cells[2].FindControl("TextBox1");
if (box.Text == "")
{
try
{
x = Convert.ToInt32(Session["ct"].ToString());
x++;
Session["ct"] = x.ToString();
}
catch
{
x = 0;
Session["ct"] = x.ToString();
}
}
if (ddl.SelectedIndex != 0)
{
string connection_string = ConfigurationManager.ConnectionStrings["DBC"].ConnectionString;
SqlConnection con = new SqlConnection(connection_string);
DropDownList box1 = (DropDownList)GridView1.Rows[x].Cells[1].FindControl("DropDownList1");
TextBox box2 = (TextBox)GridView1.Rows[x].Cells[2].FindControl("TextBox1");
TextBox box3 = (TextBox)GridView1.Rows[x].Cells[3].FindControl("TextBox2");
TextBox box4 = (TextBox)GridView1.Rows[x].Cells[4].FindControl("TextBox3");
TextBox box5 = (TextBox)GridView1.Rows[x].Cells[5].FindControl("TextBox4");
TextBox box6 = (TextBox)GridView1.Rows[x].Cells[6].FindControl("TextBox5");
TextBox box7 = (TextBox)GridView1.Rows[x].Cells[7].FindControl("TextBox6");
TextBox box8 = (TextBox)GridView1.Rows[x].Cells[8].FindControl("TextBox7");
SqlCommand cmd = new SqlCommand("select * from stud_table where Rollno='" + box1.Text + "'", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
box2.Text = ds.Tables[0].Rows[0][1].ToString();
box3.Text = ds.Tables[0].Rows[0][2].ToString();
box4.Text = ds.Tables[0].Rows[0][3].ToString();
box5.Text = ds.Tables[0].Rows[0][4].ToString();
box6.Text = ds.Tables[0].Rows[0][5].ToString();
box7.Text = ds.Tables[0].Rows[0][6].ToString();
box8.Text = ds.Tables[0].Rows[0][7].ToString();
}
else
{
// box.Text = "";
Response.Write("<Script>alert('Please Select Item Description')</script>");
}
}
}
}
我想選擇一行,如果我點擊刪除按鈕,選定的行應該被刪除。
感謝顯示你的代碼!但是,我沒有看到有任何嘗試在這裏執行刪除操作。你試過什麼了?你的方法怎麼樣都行不通?如果你還沒有嘗試過任何東西,那麼在Stack Overflow和Internet上的其他地方有很多從GridView刪除的例子。 – jadarnel27 2013-04-10 13:27:09
我認爲你應該使用網格視圖行命令事件和傳遞命令參數和命令名稱屬性按鈕..... – Rajpurohit 2013-04-10 13:34:32
我試過,但首先我想選擇行後選擇我想通過使用按鈕刪除 – divi 2013-04-11 06:30:14