我有一个gridview,生成的输出是
但我想删除 String spreadsheetId = "my spread sheet ID";
String range = "Class Data!A2:A4";
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
列,并希望这种类型的输出
永远不要更改SQL查询,因为这样查询就可以根据我的逻辑运行。我还想在每个文本框中删除Column_Name
,如何通过使用循环并保存在SQL Server数据库中来获取所有gridview数据?
ASPX:
 
代码隐藏:
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server">
<form id="form" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
onrowdatabound="GridView1_RowDataBound">
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="load" Text="gridview" />
<asp:Button ID="Button2" runat="server" OnClick="show" Text="Button" />
<asp:Button ID="Button3" runat="server" OnClick="save" Text="Save" />
</form>
</asp:Content>
答案 0 :(得分:1)
更改您的Codebehind代码:
protected void Page_Load(object sender, EventArgs e)
{
If(!IsPostBack)
{
string query = "USE db_compiler SELECT Column_Name FROM tbl_field WHERE Table_Name='deptt'";
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
DataTable dt = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adp.Fill(dt);
Session["num"] = dt.Rows.Count;
for (int i = 0; i < dt.Rows.Count; i++)
{
DataColumn dc = new DataColumn(dt.Rows[i]["Name"].ToString());
table.Columns.Add(dc); // Make this Column in 2nd DataTable and bind that to Gridview
}
DataRow r = table.NewRow();
Session["table"] = dt;
table.Rows.Add(r); // Add as many row you want to add with for loop
Gridview1.DataSource = table;
Gridview1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
TextBox txt = new TextBox();
e.Row.Cells[i].Controls.Add(txt);
}
}
}
答案 1 :(得分:0)
您可以尝试使用此修改后的RowDataBound
事件处理程序:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 1; i < e.Row.Cells.Count; i++) // Does not process the first cell
{
TextBox txt = new TextBox();
TableCell cell = e.Row.Cells[i];
txt.Text = cell.Text.Replace(" ", ""); // Removes
cell.Text = "";
cell.Controls.Add(txt);
}
}
e.Row.Cells.RemoveAt(0); // Removes the first cell in the row
}