我尝试完成按钮单击操作以在gridview中实现选中行,下面是我的asp.net代码
如果我单击“编辑”按钮,我想将gridview行,列值分配到它们的文本框中。如何用代码解释?
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="505px" OnSelectedIndexChanged="GridView_SelectedIndexChanged" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</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" />
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</form>
C#
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data source =;Database = ;Integrated security =true");
SqlCommand cmd = new SqlCommand("select * from Empdetails", con);
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Dispose();
con.Close();
}
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
{
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("CheckBox1") as CheckBox).Checked)
{
TextBox1.Text = row.Cells[0].Text;
TextBox2.Text = row.Cells[1].Text;
TextBox3.Text = row.Cells[2].Text;
}
}
}
}
}
这里使用Empid是主列和其他2列......但它没有正确执行
答案 0 :(得分:0)
代码背后。我正在创建一个数据集来绑定到gridview并在其中放置一些虚拟值。如果您不确定如何将数据绑定到gridview,那么谷歌就可以了,这里有很多例子! :)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var emps = new DataTable("Employee");
emps.Columns.Add(new DataColumn() {ColumnName = "EmpId", DataType = Type.GetType("System.Int32")});
emps.Columns.Add(new DataColumn() {ColumnName = "Name", DataType = Type.GetType("System.String")});
emps.Columns.Add(new DataColumn() {ColumnName = "Address", DataType = Type.GetType("System.String")});
var dataSet = new DataSet();
dataSet.Tables.Add(emps);
var row = emps.NewRow();
row["EmpId"] = 1000;
row["Name"] = "John";
row["Address"] = "1 Smith St";
emps.Rows.Add(row);
GridView1.DataSource = dataSet;
GridView1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("CheckBox1") as CheckBox).Checked)
{
TextBox1.Text = row.Cells[1].Text;
TextBox2.Text = row.Cells[2].Text;
TextBox3.Text = row.Cells[3].Text;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
这是您的aspx页面的代码
<div>
<asp:Label ID="Label1" runat="server" Text="Empid" Width="50px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Name" Width="50px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Address" Width="50px"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
<br />
<br />
<asp:GridView ID="GridView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /></div> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="update " /> <asp:Button ID="Button2" runat="server" Text="Edit" Width="61px" OnClick="Button2_Click" />
答案 1 :(得分:0)
你可以尝试这个,可能是可能的答案。
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
TextBox1.Text = row.Cells[1].Text;
TextBox2.Text = row.Cells[2].Text;
TextBox3.Text = row.Cells[3].Text;
}
}
答案 2 :(得分:0)
如果您尝试将选中CheckBox的行的GridView(名为GridView1)单元格值分配给三个文本框,则下面是修复。
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("CheckBox1") as CheckBox).Checked)
{
TextBox1.Text = row.Cells[0].Text;
TextBox2.Text = row.Cells[1].Text;
TextBox3.Text = row.Cells[2].Text;
}
}
}
和最重要的是,请将aspx中的GridView命名为&#39; GridView1&#39; ,因为你不能拥有ID的类名
答案 3 :(得分:0)
我想将'gridview行值'添加到文本框中,因此没有对此问题的答案;分享要阅读的内容,退回到此