如何在单独的按钮单击事件中访问gridview数据
本文关键字:事件 访问 gridview 数据 单击 按钮 单独 | 更新日期: 2023-09-27 18:01:42
我的要求是我需要访问一个按钮单击事件中的网格视图数据。我有网格视图与名称gvData和有按钮命名更新。在更新按钮的点击我保存的数据在网格中输入的用户到数据库。请建议我做这件事。代码片段会有很大的帮助。我正在填充gridview页面加载:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt;
String SQL = "SELECT * FROM tbl_Class";
string sConstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(sConstr))
{
using (SqlCommand comm = new SqlCommand(SQL, conn))
{
conn.Open();
using (SqlDataAdapter da = new SqlDataAdapter(comm))
{
dt = new DataTable();
da.Fill(dt);
}
}
}
gvMarks.DataSource = dt;
gvMarks.DataBind();
}
}
<asp:GridView ID="gvMarks" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name"/>
<asp:TemplateField HeaderText="One">
<ItemTemplate>
<asp:TextBox ID="txt1" runat="server" Text='<%# Bind("One") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Two">
<ItemTemplate>
<asp:TextBox ID="txt2" runat="server" Text='<%# Bind("TWO") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Three">
<ItemTemplate>
<asp:TextBox ID="txtThree" runat="server" Text='<%# Bind("THREE") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Four">
<ItemTemplate>
<asp:TextBox ID="txt4" runat="server" Text='<%# Bind("FOUR") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FIVE">
<ItemTemplate>
<asp:TextBox ID="txtFive" runat="server" Text='<%# Bind("FIVE") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SIX">
<ItemTemplate>
<asp:TextBox ID="txt6" runat="server" Text='<%# Bind("SIX") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnUpdae" runat="server" Text="Update Marks" OnClick="btnUpdae_Click" />
protected void btnUpdae_Click(object sender, EventArgs e)
{
//Need to acces the grid view rows and colums here
}
通过foreach或for循环逐个获取行并保存数据。点击This了解更多关于将gridview数据保存到数据库的信息
您可以使用以下方法-
TextBox txt=(TextBox)gvMarks.Rows[i].Cells[0].FindControl("txt1");
string txtOne= txt.Text;
可以与for循环和所有文本框一起使用。然后将字符串变量(如txtOne)中获取的数据插入/更新到表中。然后再次绑定网格。您可以点击以下链接获取另一个解决方案。
http://forums.asp.net/t/1647616.aspx?Get%20Value%20of%20TextBox%20inside%20the%20GridView%20in%20c%20我希望这能帮助你完成工作。
试试这个。
protected void btnUpdae_Click(object sender, EventArgs e)
{
int rowIndex = 0;
StringCollection sc = new StringCollection();
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)gvMarks.Rows[rowIndex].Cells[0].FindControl("txt1");
TextBox box2 = (TextBox)gvMarks.Rows[rowIndex].Cells[1].FindControl("txt2");
TextBox box3 = (TextBox)gvMarks.Rows[rowIndex].Cells[2].FindControl("txt3");
TextBox box4 = (TextBox)gvMarks.Rows[rowIndex].Cells[3].FindControl("txt4");
TextBox box5 = (TextBox)gvMarks.Rows[rowIndex].Cells[4].FindControl("txt5");
TextBox box6 = (TextBox)gvMarks.Rows[rowIndex].Cells[5].FindControl("txt6");
//get the values from the TextBoxes
//then add it to the collections with a comma "," as the delimited values
sc.Add(box1.Text + "," + box2.Text + "," + box3.Text+ "," + box4.Text + "," + box5.Text+ "," + box6.Text);
rowIndex++;
}
//Call the method for executing inserts or update
}
}
}