如何删除GridView的第一行

时间:2019-11-22 13:42:17

标签: c# gridview datatable row delete-row

我有一个gridview,当按下2个按钮时就会填充它。

https://www.easyallies.com/api/review/get

使用import requests from bs4 import BeautifulSoup data = { 'method': 'review', 'action': 'get', 'data[start]': 0, 'data[limit]': 10 } reviews = requests.post("https://www.easyallies.com/api/review/get", data=data) print (reviews.text) 函数,用新行创建 <asp:GridView ID="GridView1" runat="server" ShowFooter="True" AutoGenerateColumns="False" Width="100%" Height="100%" HeaderStyle-BackColor="#3F51B5" HeaderStyle-ForeColor="White" BorderWidth="1px"> <Columns> <asp:BoundField DataField="RowNumber" HeaderText="Nr."> <ControlStyle BorderStyle="None" /> </asp:BoundField> <asp:BoundField DataField="Message" HeaderText="Message"> <ControlStyle BorderStyle="None" /> </asp:BoundField> <asp:BoundField DataField="Data" HeaderText="Data"> <ControlStyle BorderStyle="None" /> </asp:BoundField> <asp:BoundField DataField="ClasaMesaj" HeaderText="ClasaMesaj"> <ControlStyle BorderStyle="None" /> </asp:BoundField> </Columns> </asp:GridView> FirstGridViewRow()GridView1

中被调用
FirstGridViewRow()

单击两个按钮之一,每次都添加新行。

PageLoad

因此,在加载页面时, protected void Page_Load(object sender, EventArgs e) { if (this.IsPostBack) return; FirstGridViewRow(); } private void FirstGridViewRow() { DataTable dt = new DataTable(); DataRow dr = null; dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Message", typeof(string))); dt.Columns.Add(new DataColumn("Data", typeof(string))); dt.Columns.Add(new DataColumn("ClasaMesaj", typeof(string))); dr = dt.NewRow(); dr["RowNumber"] = 1; dr["Message"] = "";// string.Empty; dr["Data"] = "";// string.Empty; dr["ClasaMesaj"] = 0; dt.Rows.Add(dr); // dt.Rows.Remove(dt.Rows[0]); //I have tried like this, but no row would be added ViewState["CurrentTable"] = dt; GridView1.DataSource = dt; GridView1.DataBind(); } 有1行(在 private void AddNewRow(string s, string s1, int ClasaMesaj) { int rowIndex = 1; DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = dtCurrentTable.NewRow(); // null;// dtCurrentTable.NewRow(); for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { drCurrentRow["RowNumber"] = i+1; drCurrentRow[1] = s;// "s"; drCurrentRow[2] = s1;// "s1"; drCurrentRow[3] = ClasaMesaj; rowIndex++; } dtCurrentTable.Rows.Add(drCurrentRow); ViewState["CurrentTable"] = dtCurrentTable; GridView1.DataSource = dtCurrentTable; GridView1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { clsMsg = 1; DateTime data = DateTime.Now; string dt = data.ToString(); AddNewRow("Start", dt, clsMsg); } protected void Button2_Click(object sender, EventArgs e) { clsMsg = 2; DateTime data = DateTime.Now; string dt = data.ToString(); AddNewRow("Stop", dt, clsMsg); } 中初始化的行)。当我按下其中一个按钮时,将创建第2行。我想删除初始化的行,或者在按下按钮时用信息填充它。我该怎么办?我试图摆脱GridView1并仅使用FirstGridViewRow(),但没有成功。

1 个答案:

答案 0 :(得分:0)

尝试一下。我已经在您的代码中为您添加了删除第一行。

static bool first = true;
       private void AddNewRow(string s, string s1, int ClasaMesaj)
{
    int rowIndex = 1;
    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
    DataRow drCurrentRow = dtCurrentTable.NewRow(); // null;// 
 dtCurrentTable.NewRow();

    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
    {
        drCurrentRow["RowNumber"] = i+1;
        drCurrentRow[1] = s;// "s";
        drCurrentRow[2] = s1;// "s1";
        drCurrentRow[3] = ClasaMesaj;
        rowIndex++;
    }
    dtCurrentTable.Rows.Add(drCurrentRow);
    ViewState["CurrentTable"] = dtCurrentTable;




if (first == true){
dtCurrentTable.Rows.RemoveAt(0);
first = false;
}



    GridView1.DataSource = dtCurrentTable;
    GridView1.DataBind();
}

不过只是一个问题。仅在第一次按下按钮时或每次都需要删除第一行吗?