【问题标题】:how can i remove the gridview column in asp.net c#?如何删除 asp.net c# 中的 gridview 列?
【发布时间】:2016-06-18 05:16:44
【问题描述】:

我有一个gridview,生成的输出是

但我想删除Column_Name 列并想要这种类型的输出

永远不要更改 SQL 查询,因为这样查询会根据我的逻辑工作。而且我还想删除每个文本框中的&nbsp,如何使用循环获取所有gridview数据并保存在SQL Server数据库中?

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>

代码隐藏:

 SqlConnection cnn = new SqlConnection("Data Source=LIFE_WELL;Initial Catalog=db_compiler;Integrated Security=True");

protected void Page_Load(object sender, EventArgs e)
{
    string query = "USE db_compiler SELECT Column_Name FROM tbl_field WHERE Table_Name='deptt'";

    SqlCommand cmd = new SqlCommand(query, cnn);

    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]["Column_Name"].ToString());
        dt.Columns.Add(dc);
    }

    DataRow r = table.NewRow();

    GridView1.DataSource = table;
    GridView1.DataBind();

    cnn.Close();
    cnn.Open();

    cmd.ExecuteNonQuery();
    cnn.Close();

    Session["table"] = dt;

    GridView1.DataSource = dt;
    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();
            txt.Text = e.Row.Cells[i].Text;
            e.Row.Cells[i].Text = "";
            e.Row.Cells[i].Controls.Add(txt);
        }
    }
}

【问题讨论】:

  • 设置“AutogenerateColumns=false”并定义您自己的列....
  • 你能在我的代码中做一些改变吗???我的代码工作得很好。你能在gridview中隐藏Column_Name列,当从gridview获取数据时column_name数据永远不会插入。谢谢跨度>

标签: c# asp.net gridview sql-server-2012


【解决方案1】:

更改您的代码隐藏代码:

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);
        }
    }
}

【讨论】:

  • 感谢您的回复。如果我使用AutoGenerateColumn=false,那么我的gridview 消失了。输出中没有显示gridview,第二件事是我不能使用TemplateField,因为我的gridview 列是动态生成的。它可能是b 2,3,5 或 10.不知道 gridview 标题和多少列。
  • 根据您的问题,我只是更新编码尝试过,如果有任何问题告诉我。
  • 快乐是我的。我很高兴它对您有所帮助。
【解决方案2】:

您可以尝试使用这个修改后的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("&nbsp;", ""); // Removes &nbsp;
            cell.Text = "";
            cell.Controls.Add(txt);
        }
    }

    e.Row.Cells.RemoveAt(0); // Removes the first cell in the row
}

【讨论】:

    猜你喜欢
    • 2014-05-06
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多