如何为GridView中的特定行删除行或禁用删除按钮? [英] How to Delete a Row or Disable Delete Button for specific row in GridView?

查看:120
本文介绍了如何为GridView中的特定行删除行或禁用删除按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁用 GridView 删除 按钮删除 for rowindex = 0

/ code> a 行 on 删除 按钮点击。



我的 GridView 仅用于获取数据。我正在使用ak创建新行并将其作为 DataSource 用于 GridView

 < asp:gridview ID =Gridview1runat =serverShowFooter =trueAutoGenerateColumns =falseAutoGenerateDeleteButton =trueOnRowDeleting =Gridview1_RowDeleting > 
<列>
< asp:BoundField DataField =RowNumberHeaderText =Row Number/>
< asp:TemplateField HeaderText =WorkDone>
< ItemTemplate>
< asp:TextBox ID =txtWorkDonerunat =server>< / asp:TextBox>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =花费的总时间>
< ItemTemplate>
< asp:DropDownList runat =serverID =ddlHours>
< asp:ListItem Selected =True> 1< / asp:ListItem>
< asp:ListItem> 2< / asp:ListItem>
< asp:ListItem> 3< / asp:ListItem>
< asp:ListItem> 4< / asp:ListItem>
< asp:ListItem> 5< / asp:ListItem>
< asp:ListItem> 6< / asp:ListItem>
< asp:ListItem> 7< / asp:ListItem>
< asp:ListItem> 8< / asp:ListItem>
< asp:ListItem> 9< / asp:ListItem>
< / asp:DropDownList>

< / ItemTemplate>
< FooterStyle Horizo​​ntalAlign =Right/>
< FooterTemplate>
< asp:Button ID =btnAddNewRowrunat =serverText =Add New Row
onclick =btnAddNewRow_ClickCausesValidation =false/>
< / FooterTemplate>
< / asp:TemplateField>

< /列>
< / asp:gridview>

但我找不到在事件 RowDeleting
pre $ protected void Gridview1_RowDeleting(object sender,GridViewDeleteEventArgs e)
{
// Here我没有得到
}



private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn(RowNumber,typeof(string)));
dt.Columns.Add(new DataColumn(WorkDone,typeof(string)));
dt.Columns.Add(new DataColumn(TimeSpent,typeof(string)));
dr = dt.NewRow();
dr [RowNumber] = 1;
dr [WorkDone] = string.Empty;
dr [TimeSpent] = string.Empty;
dt.Rows.Add(dr);

//将DataTable存储在ViewState中
ViewState [CurrentTable] = dt;

Gridview1.DataSource = dt;
Gridview1.DataBind();
foreach(GridViewRow在Gridview1.Rows中的行)
{
if(row.RowIndex == 0)
{
//删除按钮必须被禁用
}
}
}

private void AddNewRowToGrid()
{
int rowIndex = 0; (ViewState [CurrentTable]!= null)

DataTable dtCurrentTable =(DataTable)ViewState [CurrentTable];
DataRow drCurrentRow = null;
if(dtCurrentTable.Rows.Count> 0)
{
for(int i = 1; i <= dtCurrentTable.Rows.Count; i ++)
{
//提取文本框值
TextBox box1 =(TextBox)Gridview1.Rows [rowIndex] .Cells [1] .FindControl(txtWorkDone);
DropDownList box2 =(DropDownList)Gridview1.Rows [rowIndex] .Cells [2] .FindControl(ddlHours);

drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow [RowNumber] = i + 1;

dtCurrentTable.Rows [i - 1] [WorkDone] = box1.Text;
dtCurrentTable.Rows [i - 1] [TimeSpent] = box2.SelectedItem.Text;


rowIndex ++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState [CurrentTable] = dtCurrentTable;

Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write(ViewState is null);
}

//在回传中设置以前的数据
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if(ViewState [CurrentTable]!= null)
{
DataTable dt =(DataTable)ViewState [CurrentTable];
if(dt.Rows.Count> 0)
{
for(int i = 0; i< dt.Rows.Count; i ++)
{
TextBox box1 =(TextBox)Gridview1.Rows [rowIndex] .Cells [1] .FindControl(txtWorkDone);
DropDownList box2 =(DropDownList)Gridview1.Rows [rowIndex] .Cells [2] .FindControl(ddlHours);
box1.Text = dt.Rows [i] [WorkDone]。ToString();
box2.Text = dt.Rows [i] [TimeSpent]。ToString();
rowIndex ++;






帮助请任何一个!

解决方案

试试这个

  #regionVCoupons
private void SetInitialRow()
{

DataTable dt = new DataTable();
DataRow dr = null;
//dt.Columns.Add(new DataColumn(RowNumber,typeof(string)));
dt.Columns.Add(new DataColumn(Column1,typeof(string)));
dt.Columns.Add(new DataColumn(Column2,typeof(string)));
dt.Columns.Add(new DataColumn(Column3,typeof(string)));
dt.Columns.Add(new DataColumn(Column4,typeof(string)));


dr = dt.NewRow();
// dr [RowNumber] = 1;

dt.Rows.Add(dr);
dt.TableName =Coupons;
//将DataTable存储在ViewState中

ViewState [dt_CouponData] = dt;

GridviewCoupon.DataSource = dt;
GridviewCoupon.DataBind();


$ b protected void ButtonAdd_Click(object sender,EventArgs e)
{

AddNewRowToGrid();
}
private void AddNewRowToGrid()
{

int rowIndex = 0;
if(ViewState [dt_CouponData]!= null)
{
DataTable dt_CouponData =(DataTable)ViewState [dt_CouponData];
DataRow drCurrentRow = null;
if(dt_CouponData.Rows.Count> 0)
{


for(int i = 1; i <= dt_CouponData.Rows.Count; i ++ )
{
//提取文本框值


TextBox box1 =(TextBox)GridviewCoupon.Rows [rowIndex] .Cells [1] .FindControl(TextBox1 );
TextBox box2 =(TextBox)GridviewCoupon.Rows [rowIndex] .Cells [2] .FindControl(TextBox2);
TextBox box3 =(TextBox)GridviewCoupon.Rows [rowIndex] .Cells [3] .FindControl(TextBox3);
TextBox box4 =(TextBox)GridviewCoupon.Rows [rowIndex] .Cells [4] .FindControl(TextBox4);




drCurrentRow = dt_CouponData.NewRow();

// drCurrentRow [RowNumber] = i + 1;
drCurrentRow [Column1] = box1.Text;
drCurrentRow [Column2] = box2.Text;
drCurrentRow [Column3] = box3.Text;
drCurrentRow [Column4] = box4.Text;
rowIndex ++;

}

//向DataTable添加新行
dt_CouponData.Rows.Add(drCurrentRow);
//将当前数据存储到ViewState

ViewState [dt_CouponData] = dt_CouponData;

//用当前数据重新绑定网格
GridviewCoupon.DataSource = dt_CouponData;
GridviewCoupon.DataBind();
}
}
else
{
Response.Write(ViewState is null);
}
DataTable dt =(DataTable)ViewState [dt_CouponData];
//在回传中设置以前的数据
int n = dt.Rows.Count;如果(dt.Rows [0] [Column1]。ToString()==&& dt.Rows [0] [Column2]。ToString()==

if && dt.Rows [0] [Column3]。ToString()==&& dt.Rows [0] [Column4]。ToString()==)
{
SetPreviousData();
}
else
{
SetData();


private void SetPreviousData()
{

int rowIndex = 0;
if(ViewState [dt_CouponData]!= null)
{
DataTable dt =(DataTable)ViewState [dt_CouponData];
if(dt.Rows.Count> 0)
{
for(int i = 1; i< dt.Rows.Count; i ++)
{
TextBox box1 =(TextBox)GridviewCoupon.Rows [rowIndex] .Cells [1] .FindControl(TextBox1);
TextBox box2 =(TextBox)GridviewCoupon.Rows [rowIndex] .Cells [2] .FindControl(TextBox2);
TextBox box3 =(TextBox)GridviewCoupon.Rows [rowIndex] .Cells [3] .FindControl(TextBox3);
TextBox box4 =(TextBox)GridviewCoupon.Rows [rowIndex] .Cells [3] .FindControl(TextBox4);

box1.Text = dt.Rows [i] [Column1]。ToString();
box2.Text = dt.Rows [i] [Column2]。ToString();
box3.Text = dt.Rows [i] [Column3]。ToString();
box4.Text = dt.Rows [i] [Column4]。ToString();

rowIndex ++;





private void SetData()
{

int rowIndex = 0;
if(ViewState [dt_CouponData]!= null)
{
DataTable dt =(DataTable)ViewState [dt_CouponData];

int n = dt.Rows.Count; (int i = 0; i {
for(int j = 0; j< n-i-1; j ++ )
{
if(dt.Rows [j] [Column1]。ToString()== dt.Rows [j + 1] [Column1]。ToString()&& dt.Rows [j] [Column2]。ToString()== dt.Rows [j + 1] [Column2]。ToString()&& dt.Rows [j] [Column3]。 ToString()== dt.Rows [j + 1] [Column3]。ToString()&& dt.Rows [j] [Column4]。ToString()== dt.Rows [j + 1 ] [Column4]。ToString())
{
dt.Rows [j + 1] [Column1] =;
dt.Rows [j + 1] [Column2] =;
dt.Rows [j + 1] [Column3] =;
dt.Rows [j + 1] [Column4] =;



$ b if(dt.Rows [n - 2] [Column1]。ToString()==&& dt.Rows [n - 2] [Column2]。ToString()==&& dt.Rows [n - 2] [Column3]。ToString()==&& dt.Rows [n - 2] [Column4]。ToString()==)
{
dt.Rows [n - 2] [Column1] = dt.Rows [n - 1] [Column1];
dt.Rows [n - 2] [Column2] = dt.Rows [n - 1] [Column2];
dt.Rows [n - 2] [Column3] = dt.Rows [n - 1] [Column3];
dt.Rows [n - 2] [Column4] = dt.Rows [n - 1] [Column4];

dt.Rows [n - 1] [Column1] =;
dt.Rows [n - 1] [Column2] =;
dt.Rows [n - 1] [Column3] =;
dt.Rows [n - 1] [Column4] =;如果(dt.Rows.Count> 0)
{
for(int i = 0; i< dt.Rows.Count - 1),


; i ++)
{
TextBox box1 =(TextBox)GridviewCoupon.Rows [i] .Cells [1] .FindControl(TextBox1);
TextBox box2 =(TextBox)GridviewCoupon.Rows [i] .Cells [2] .FindControl(TextBox2);
TextBox box3 =(TextBox)GridviewCoupon.Rows [i] .Cells [3] .FindControl(TextBox3);
TextBox box4 =(TextBox)GridviewCoupon.Rows [i] .Cells [3] .FindControl(TextBox4);

box1.Text = dt.Rows [i] [Column1]。ToString();
box2.Text = dt.Rows [i] [Column2]。ToString();
box3.Text = dt.Rows [i] [Column3]。ToString();
box4.Text = dt.Rows [i] [Column4]。ToString();

rowIndex ++;

}

}
int b = dt.Rows.Count;


//用当前数据重新绑定网格


$ b private void SaveCoupons()
{

// int rowIndex = 0;
if(ViewState [dt_CouponData]!= null)
{
DataTable dt_CouponData =(DataTable)ViewState [dt_CouponData];
DataRow drCurrentRow = null;如果(dt_CouponData.Rows.Count> 0)
{


for(int i = 0; i< dt_CouponData.Rows.Count; i ++)
{
//提取文本框值


TextBox box1 =(TextBox)GridviewCoupon.Rows [i] .Cells [1] .FindControl(TextBox1 );
TextBox box2 =(TextBox)GridviewCoupon.Rows [i] .Cells [2] .FindControl(TextBox2);
TextBox box3 =(TextBox)GridviewCoupon.Rows [i] .Cells [3] .FindControl(TextBox3);
TextBox box4 =(TextBox)GridviewCoupon.Rows [i] .Cells [4] .FindControl(TextBox4);




drCurrentRow = dt_CouponData.NewRow();

// drCurrentRow [RowNumber] = i + 1;
drCurrentRow [Column1] = box1.Text;
drCurrentRow [Column2] = box2.Text;
drCurrentRow [Column3] = box3.Text;
drCurrentRow [Column4] = box4.Text;
// rowIndex ++;

}

dt_CouponData.Rows.Add(drCurrentRow);
//将新行添加到DataTable

//将当前数据存储到ViewState
ViewState [dt_CouponData] = dt_CouponData;


$ b}
}
else
{
Response.Write(ViewState is null);




$ b private void Save()
{

int rowIndex = 0;
if(ViewState [dt_CouponData]!= null)
{
DataTable dt_CouponData =(DataTable)ViewState [dt_CouponData];
DataRow drCurrentRow = null;如果(dt_CouponData.Rows.Count> 0)
{


for(int i = 0; i< dt_CouponData.Rows.Count; i ++)
{
//提取文本框值


TextBox box1 =(TextBox)GridviewCoupon.Rows [i] .Cells [1] .FindControl(TextBox1 );
TextBox box2 =(TextBox)GridviewCoupon.Rows [i] .Cells [2] .FindControl(TextBox2);
TextBox box3 =(TextBox)GridviewCoupon.Rows [i] .Cells [3] .FindControl(TextBox3);
TextBox box4 =(TextBox)GridviewCoupon.Rows [i] .Cells [4] .FindControl(TextBox4);




drCurrentRow = dt_CouponData.NewRow();

// drCurrentRow [RowNumber] = i + 1;
drCurrentRow [Column1] = box1.Text;
drCurrentRow [Column2] = box2.Text;
drCurrentRow [Column3] = box3.Text;
drCurrentRow [Column4] = box4.Text;
// rowIndex ++;

}

//向DataTable添加新行
dt_CouponData.Rows.Add(drCurrentRow);
int n = dt_CouponData.Rows.Count;
if(dt_CouponData.Rows [n - 2] [Column1]。ToString()==&& dt_CouponData.Rows [n - 2] [Column2]。 && dt_CouponData.Rows [n - 2] [Column3]。ToString()==&& dt_CouponData.Rows [n - 2] [Column4]。 )
{
dt_CouponData.Rows [n - 2] [Column1] = dt_CouponData.Rows [n - 1] [Column1];
dt_CouponData.Rows [n - 2] [Column2] = dt_CouponData.Rows [n - 1] [Column2];
dt_CouponData.Rows [n - 2] [Column3] = dt_CouponData.Rows [n - 1] [Column3];
dt_CouponData.Rows [n - 2] [Column4] = dt_CouponData.Rows [n - 1] [Column4];

dt_CouponData.Rows [n - 1] .Delete();

}
int b = dt_CouponData.Rows.Count;


int a = b - 1;
if(dt_CouponData.Rows [a] [Column1]。ToString()== dt_CouponData.Rows [a - 1] [Column1]。ToString()&& dt_CouponData.Rows [a] [Column2]。ToString()== dt_CouponData.Rows [a - 1] [Column2]。ToString()&& dt_CouponData.Rows [a] [Column3]。ToString()== dt_CouponData .Rows [a - 1] [Column3]。ToString()& dt_CouponData.Rows [a] [Column4]。ToString()== dt_CouponData.Rows [a - 1] [Column4] .ToString())
{
dt_CouponData.Rows [a] .Delete();
}
//将当前数据存储到ViewState
ViewState [dt_CouponData] = dt_CouponData;


$ b}
}
else
{
Response.Write(ViewState is null);




$ b private void SetPrevious()
{

int rowIndex = 0;
if(ViewState [dt_CouponData]!= null)
{
DataTable dt =(DataTable)ViewState [dt_CouponData];
int o = 1;
int b = dt.Rows.Count;
int a = b - 1;
GridviewCoupon.DataSource = dt;
if(dt.Rows [0] [Column2]。ToString()==)
{
dt.Rows [0] .Delete();
}

// else if(dt.Rows [a] [Column1]。ToString()== dt.Rows [a - 1] [Column1]。ToString ()&& dt.Rows [a] [Column2]。ToString()== dt.Rows [a - 1] [Column2]。ToString()&& dt.Rows [a] [Column3]。ToString()== dt.Rows [a - 1] [Column3]。ToString()&& dt.Rows [a] [Column4]。ToString()== dt .Rows [a - 1] [Column4]。ToString())
// {
// dt.Rows [a] .Delete();
//}
// if(dt.Rows [o] [Column1]。ToString()== dt.Rows [o - 1] [Column1]。ToString()& amp ;& dt.Rows [o] [Column2]。ToString()== dt.Rows [o - 1] [Column2]。ToString()& dt.Rows [o] [Column3 ] .ToString()== dt.Rows [o - 1] [Column3]。ToString()&& dt.Rows [o] [Column4]。ToString()== dt.Rows [ o - 1] [Column4]。ToString())
// {
// dt.Rows [0] .Delete();
//}
GridviewCoupon.DataBind();
if(dt.Rows.Count> 0)
{
int j = dt.Rows.Count;

ViewState [dt_CouponData] = dt;


(int i = dt.Rows.Count - 1; i> = 0; i--)
{
TextBox box1 =(TextBox) GridviewCoupon.Rows [I] .Cells [1] .FindControl( TextBox1中);
TextBox box2 =(TextBox)GridviewCoupon.Rows [i] .Cells [2] .FindControl(TextBox2);
TextBox box3 =(TextBox)GridviewCoupon.Rows [i] .Cells [3] .FindControl(TextBox3);
TextBox box4 =(TextBox)GridviewCoupon.Rows [i] .Cells [4] .FindControl(TextBox4);

box1.Text = dt.Rows [i] [Column1]。ToString();
box2.Text = dt.Rows [i] [Column2]。ToString();
box3.Text = dt.Rows [i] [Column3]。ToString();
box4.Text = dt.Rows [i] [Column4]。ToString();

// if(rowIndex< dt.Rows.Count-1)
// {
// rowIndex ++;






$ b protected void GridviewCoupon_RowCommand(object sender,GridViewCommandEventArgs e)



if(e.CommandName ==More)
{
objENT = new ENT();
DataTable dt =(DataTable)ViewState [dt_CouponData];

int index = Convert.ToInt32(e.CommandArgument.ToString());
if(dt.Rows [0] [Column1]。ToString()==&& dt.Rows [0] [Column2]。ToString()==& & dt.Rows [0] [Column3]。ToString()==&& dt.Rows [0] [Column4]。ToString()==)
{
index ++;

}
Save();
int count = dt.Rows.Count;
if(count == index)
{
dt.Rows [index - 1] .Delete();
}
else
{
dt.Rows [index] .Delete();
}
ViewState [dt_CouponData] = dt;

SetPrevious();





protected void GridviewCoupon_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.RowType == DataControlRowType.DataRow)
{
Button Delete =(Button)e.Row.FindControl(btnDelete);
DataRowView dr = e.Row.DataItem作为DataRowView;
int index = e.Row.RowIndex;
if(index == 0)
{
Delete.Visible = false;
}
else
{
Delete.Visible = true;




#endregion
div class =rowstyle =margin-top:2% >
< div class =col-sm-1>< / div>
< div class =container-fluid col-sm-10>

< asp:GridView ID =GridviewCouponOnRowEditing =GridviewCoupon_RowEditingOnRowDataBound =GridviewCoupon_RowDataBoundOnRowCommand =GridviewCoupon_RowCommandrunat =serverEnabled =falseStyle =text-align: centerShowFooter =trueWidth =99%
AutoGenerateColumns =false>
<列>
<% - < asp:BoundField DataField =RowNumberHeaderText =Sr NoHeaderStyle-BackColor =#99CCCC/> - %>
<% - < asp:TemplateField HeaderText =Sr NoHeaderStyle-BackColor =#99CCCC>
< ItemTemplate>
< asp:Label ID =txtbox0Text ='<%#Bind(RowNumber)%>'runat =server>< / asp:Label>
< / ItemTemplate>
< / asp:TemplateField> - %>
<% - < asp:TemplateField HeaderText =Survey NumberHeaderStyle-BackColor =#99CCCC>
< ItemTemplate>
< asp:TextBox ID =TextBoxSCssClass =form-controlrequired =requiredrunat =server>< / asp:TextBox>
< / ItemTemplate>
< / asp:TemplateField> - %>
< asp:TemplateField HeaderText =优惠券代码HeaderStyle-BackColor =#99CCCC>
< ItemTemplate>
< asp:TextBox ID =TextBox1required =requiredCssClass =form-controlrunat =server>< / asp:TextBox>
< / ItemTemplate>
< / asp:TemplateField>

< asp:TemplateField HeaderText =优惠券说明HeaderStyle-BackColor =#99CCCC>
< ItemTemplate>
< asp:TextBox ID =TextBox2MaxLength =250required =requiredCssClass =form-controlrunat =server>< / asp:TextBox>
< / ItemTemplate>
< / asp:TemplateField>

< asp:TemplateField HeaderText =开始日期HeaderStyle-BackColor =#99CCCC>
< ItemTemplate>
< asp:TextBox ID =TextBox3required =requiredplaceholder =DD / MMM / YYYYCssClass =form-control calrunat =server>< / asp:TextBox>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField HeaderText =End DateHeaderStyle-BackColor =#99CCCC>
< ItemTemplate>
< asp:TextBox ID =TextBox4required =requiredplaceholder =DD / MM / YYYYCssClass =form-controlrunat =server>< / asp:TextBox>
< / ItemTemplate>

< / asp:TemplateField>
< asp:TemplateField HeaderText =End DateHeaderStyle-BackColor =#99CCCC>
< ItemTemplate>
< asp:Button ID =btnDeleteText =Delete
CommandArgument =<%#((GridViewRow)Container).RowIndex%>
CommandName =MoreCssClass =btn-primary btn
Style =padding-top:1%; padding-bottom:1%; margin-top:1px; margin-bottom:1px runat =server/>
< / ItemTemplate>


< FooterStyle Horizo​​ntalAlign =Right/>
< FooterTemplate>
< asp:Button ID =ButtonAddFont-Bold =truerunat =serverText =Add New RowCssClass =btn-toolbar btnOnClick =ButtonAdd_Click/>
< / FooterTemplate>
< / asp:TemplateField>
< /列>

<% - < FooterStyle BackColor =#99CCCCForeColor =#003399/> - %>
< RowStyle BackColor =WhiteForeColor =#003399/>
< SelectedRowStyle BackColor =#009999Font-Bold =TrueForeColor =#CCFF99/>
< SortedAscendingCellStyle BackColor =#EDF6F6/>
< SortedAscendingHeaderStyle BackColor =#0D4AC4/>
< SortedDescendingCellStyle BackColor =#D6DFDF/>
< SortedDescendingHeaderStyle BackColor =#002876/>
< / asp:GridView>

< / div>

这里是整个代码通过,但像1-2天一样:)
希望它有帮助


I am facing a problem in how to disable GridView Delete Button for rowindex = 0.

Secondly, I want to Delete a Row on Delete Button clicked.

My GridView is used here for taking data only. I am using a k to create new row and serve it as DataSource for GridView.

  <asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false" AutoGenerateDeleteButton="true" OnRowDeleting="Gridview1_RowDeleting">
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
            <asp:TemplateField HeaderText="WorkDone">
                <ItemTemplate>
                    <asp:TextBox ID="txtWorkDone" runat="server"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total Time spent">
                <ItemTemplate>
                    <asp:DropDownList runat="server" ID="ddlHours">
                        <asp:ListItem Selected="True">1</asp:ListItem>
                        <asp:ListItem>2</asp:ListItem>
                        <asp:ListItem>3</asp:ListItem>
                        <asp:ListItem>4</asp:ListItem>
                        <asp:ListItem>5</asp:ListItem>
                        <asp:ListItem>6</asp:ListItem>
                        <asp:ListItem>7</asp:ListItem>
                        <asp:ListItem>8</asp:ListItem>
                        <asp:ListItem>9</asp:ListItem>
                    </asp:DropDownList>

                </ItemTemplate>
                <FooterStyle HorizontalAlign="Right" />
                <FooterTemplate>
                 <asp:Button ID="btnAddNewRow" runat="server" Text="Add New Row" 
                        onclick="btnAddNewRow_Click" CausesValidation="false"/>
                </FooterTemplate>
            </asp:TemplateField>

            </Columns>
        </asp:gridview>

but I could not figure out what to implement inside event RowDeleting

protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
           //Here i am not getting
        }



private void SetInitialRow()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
            dt.Columns.Add(new DataColumn("WorkDone", typeof(string)));
            dt.Columns.Add(new DataColumn("TimeSpent", typeof(string)));
            dr = dt.NewRow();
            dr["RowNumber"] = 1;
            dr["WorkDone"] = string.Empty;
            dr["TimeSpent"] = string.Empty;
            dt.Rows.Add(dr);

            //Store the DataTable in ViewState
            ViewState["CurrentTable"] = dt;

            Gridview1.DataSource = dt;
            Gridview1.DataBind();
            foreach(GridViewRow row in Gridview1.Rows)
            {
                if(row.RowIndex == 0)
                {
                    //Delete button must be disabled
                }
            }
        }

   private void AddNewRowToGrid()
        {
            int rowIndex = 0;

            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values
                        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtWorkDone");
                        DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddlHours");

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;

                        dtCurrentTable.Rows[i - 1]["WorkDone"] = box1.Text;
                        dtCurrentTable.Rows[i - 1]["TimeSpent"] = box2.SelectedItem.Text;


                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;

                    Gridview1.DataSource = dtCurrentTable;
                    Gridview1.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }

            //Set Previous Data on Postbacks
            SetPreviousData();
        }
        private void SetPreviousData()
        {
            int rowIndex = 0;
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dt = (DataTable)ViewState["CurrentTable"];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtWorkDone");
                        DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddlHours");
                        box1.Text = dt.Rows[i]["WorkDone"].ToString();
                        box2.Text = dt.Rows[i]["TimeSpent"].ToString();
                        rowIndex++;
                    }
                }
            }
        }

Help please any one!!

解决方案

try this

#region"VCoupons"
    private void SetInitialRow()
    {

        DataTable dt = new DataTable();
        DataRow dr = null;
       //dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Column1", typeof(string)));
        dt.Columns.Add(new DataColumn("Column2", typeof(string)));
        dt.Columns.Add(new DataColumn("Column3", typeof(string)));
        dt.Columns.Add(new DataColumn("Column4", typeof(string)));


        dr = dt.NewRow();
      //  dr["RowNumber"] = 1;

        dt.Rows.Add(dr);
        dt.TableName = "Coupons";
        //Store the DataTable in ViewState

        ViewState["dt_CouponData"] = dt;

        GridviewCoupon.DataSource = dt;
        GridviewCoupon.DataBind();


    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {

        AddNewRowToGrid();
    }
    private void AddNewRowToGrid()
    {

        int rowIndex = 0;
        if (ViewState["dt_CouponData"] != null)
        {
            DataTable dt_CouponData = (DataTable)ViewState["dt_CouponData"];
            DataRow drCurrentRow = null;
            if (dt_CouponData.Rows.Count > 0)
            {


                for (int i = 1; i <= dt_CouponData.Rows.Count; i++)
                {
                    //extract the TextBox values


                    TextBox box1 = (TextBox)GridviewCoupon.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridviewCoupon.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridviewCoupon.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)GridviewCoupon.Rows[rowIndex].Cells[4].FindControl("TextBox4");




                    drCurrentRow = dt_CouponData.NewRow();

                    //drCurrentRow["RowNumber"] = i + 1;
                    drCurrentRow["Column1"] = box1.Text;
                    drCurrentRow["Column2"] = box2.Text;
                    drCurrentRow["Column3"] = box3.Text;
                    drCurrentRow["Column4"] = box4.Text;
                    rowIndex++;

                }

                //add new row to DataTable
                dt_CouponData.Rows.Add(drCurrentRow);
                //Store the current data to ViewState

                ViewState["dt_CouponData"] = dt_CouponData;

                //Rebind the Grid with the current data
                GridviewCoupon.DataSource = dt_CouponData;
                GridviewCoupon.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        DataTable dt = (DataTable)ViewState["dt_CouponData"];
        //Set Previous Data on Postbacks
        int n = dt.Rows.Count;

        if (dt.Rows[0]["Column1"].ToString() == "" && dt.Rows[0]["Column2"].ToString() == "" && dt.Rows[0]["Column3"].ToString() == "" && dt.Rows[0]["Column4"].ToString() == "")
        {
            SetPreviousData();
        }
        else
        {
            SetData();
        }
    }
    private void SetPreviousData()
    {

        int rowIndex = 0;
        if (ViewState["dt_CouponData"] != null)
        {
            DataTable dt = (DataTable)ViewState["dt_CouponData"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    TextBox box1 = (TextBox)GridviewCoupon.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridviewCoupon.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridviewCoupon.Rows[rowIndex].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)GridviewCoupon.Rows[rowIndex].Cells[3].FindControl("TextBox4");

                    box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();
                    box4.Text = dt.Rows[i]["Column4"].ToString();

                    rowIndex++;

                }

            }
        }
    }
    private void SetData()
    {

        int rowIndex = 0;
        if (ViewState["dt_CouponData"] != null)
        {
            DataTable dt = (DataTable)ViewState["dt_CouponData"];

            int n = dt.Rows.Count;

            for (int i = 0; i < n - 1; i++)
            {
                for (int j = 0; j < n - i - 1; j++)
                {
                    if (dt.Rows[j]["Column1"].ToString() == dt.Rows[j + 1]["Column1"].ToString() && dt.Rows[j]["Column2"].ToString() == dt.Rows[j + 1]["Column2"].ToString() && dt.Rows[j]["Column3"].ToString() == dt.Rows[j + 1]["Column3"].ToString() && dt.Rows[j]["Column4"].ToString() == dt.Rows[j + 1]["Column4"].ToString())
                    {
                        dt.Rows[j + 1]["Column1"] = "";
                        dt.Rows[j + 1]["Column2"] = "";
                        dt.Rows[j + 1]["Column3"] = "";
                        dt.Rows[j + 1]["Column4"] = "";

                    }
                }
            }
            if (dt.Rows[n - 2]["Column1"].ToString() == "" && dt.Rows[n - 2]["Column2"].ToString() == "" && dt.Rows[n - 2]["Column3"].ToString() == "" && dt.Rows[n - 2]["Column4"].ToString() == "")
            {
                dt.Rows[n - 2]["Column1"] = dt.Rows[n - 1]["Column1"];
                dt.Rows[n - 2]["Column2"] = dt.Rows[n - 1]["Column2"];
                dt.Rows[n - 2]["Column3"] = dt.Rows[n - 1]["Column3"];
                dt.Rows[n - 2]["Column4"] = dt.Rows[n - 1]["Column4"];

                dt.Rows[n - 1]["Column1"]="";
                dt.Rows[n - 1]["Column2"]="";
                dt.Rows[n - 1]["Column3"]="";
                dt.Rows[n - 1]["Column4"]="";

            }
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count - 1; i++)
                {
                    TextBox box1 = (TextBox)GridviewCoupon.Rows[i].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridviewCoupon.Rows[i].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridviewCoupon.Rows[i].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)GridviewCoupon.Rows[i].Cells[3].FindControl("TextBox4");

                    box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();
                    box4.Text = dt.Rows[i]["Column4"].ToString();

                    rowIndex++;

                }

            }
            int b = dt.Rows.Count;


            //Rebind the Grid with the current data

        }
    }
    private void SaveCoupons()
    {

        //int rowIndex = 0;
        if (ViewState["dt_CouponData"] != null)
        {
            DataTable dt_CouponData = (DataTable)ViewState["dt_CouponData"];
            DataRow drCurrentRow = null;
            if (dt_CouponData.Rows.Count > 0)
            {


                for (int i = 0; i < dt_CouponData.Rows.Count; i++)
                {
                    //extract the TextBox values


                    TextBox box1 = (TextBox)GridviewCoupon.Rows[i].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridviewCoupon.Rows[i].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridviewCoupon.Rows[i].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)GridviewCoupon.Rows[i].Cells[4].FindControl("TextBox4");




                    drCurrentRow = dt_CouponData.NewRow();

                   // drCurrentRow["RowNumber"] = i + 1;
                    drCurrentRow["Column1"] = box1.Text;
                    drCurrentRow["Column2"] = box2.Text;
                    drCurrentRow["Column3"] = box3.Text;
                    drCurrentRow["Column4"] = box4.Text;
                    // rowIndex++;

                }

                dt_CouponData.Rows.Add(drCurrentRow);
                //add new row to DataTable

                //Store the current data to ViewState
                ViewState["dt_CouponData"] = dt_CouponData;



            }
        }
        else
        {
            Response.Write("ViewState is null");
        }



    }
    private void Save()
    {

        int rowIndex = 0;
        if (ViewState["dt_CouponData"] != null)
        {
            DataTable dt_CouponData = (DataTable)ViewState["dt_CouponData"];
            DataRow drCurrentRow = null;
            if (dt_CouponData.Rows.Count > 0)
            {


                for (int i = 0; i < dt_CouponData.Rows.Count; i++)
                {
                    //extract the TextBox values


                    TextBox box1 = (TextBox)GridviewCoupon.Rows[i].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridviewCoupon.Rows[i].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridviewCoupon.Rows[i].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)GridviewCoupon.Rows[i].Cells[4].FindControl("TextBox4");




                    drCurrentRow = dt_CouponData.NewRow();

                    //drCurrentRow["RowNumber"] = i + 1;
                    drCurrentRow["Column1"] = box1.Text;
                    drCurrentRow["Column2"] = box2.Text;
                    drCurrentRow["Column3"] = box3.Text;
                    drCurrentRow["Column4"] = box4.Text;
                    // rowIndex++;

                }

                //add new row to DataTable
                dt_CouponData.Rows.Add(drCurrentRow);
                int n = dt_CouponData.Rows.Count;
                if (dt_CouponData.Rows[n - 2]["Column1"].ToString() == "" && dt_CouponData.Rows[n - 2]["Column2"].ToString() == "" && dt_CouponData.Rows[n - 2]["Column3"].ToString() == "" && dt_CouponData.Rows[n - 2]["Column4"].ToString() == "")
                {
                    dt_CouponData.Rows[n - 2]["Column1"] = dt_CouponData.Rows[n - 1]["Column1"];
                    dt_CouponData.Rows[n - 2]["Column2"] = dt_CouponData.Rows[n - 1]["Column2"];
                    dt_CouponData.Rows[n - 2]["Column3"] = dt_CouponData.Rows[n - 1]["Column3"];
                    dt_CouponData.Rows[n - 2]["Column4"] = dt_CouponData.Rows[n - 1]["Column4"];

                    dt_CouponData.Rows[n - 1].Delete();

                }
                int b = dt_CouponData.Rows.Count;


                int a = b - 1;
                if (dt_CouponData.Rows[a]["Column1"].ToString() == dt_CouponData.Rows[a - 1]["Column1"].ToString() && dt_CouponData.Rows[a]["Column2"].ToString() == dt_CouponData.Rows[a - 1]["Column2"].ToString() && dt_CouponData.Rows[a]["Column3"].ToString() == dt_CouponData.Rows[a - 1]["Column3"].ToString() && dt_CouponData.Rows[a]["Column4"].ToString() == dt_CouponData.Rows[a - 1]["Column4"].ToString())
                {
                    dt_CouponData.Rows[a].Delete();
                }
                //Store the current data to ViewState
                ViewState["dt_CouponData"] = dt_CouponData;



            }
        }
        else
        {
            Response.Write("ViewState is null");
        }



    }
    private void SetPrevious()
    {

        int rowIndex = 0;
        if (ViewState["dt_CouponData"] != null)
        {
            DataTable dt = (DataTable)ViewState["dt_CouponData"];
            int o = 1;
            int b = dt.Rows.Count;
            int a = b - 1;
            GridviewCoupon.DataSource = dt;
            if (dt.Rows[0]["Column2"].ToString() == "")
            {
                dt.Rows[0].Delete();
            }

            //else if (dt.Rows[a]["Column1"].ToString() == dt.Rows[a - 1]["Column1"].ToString() && dt.Rows[a]["Column2"].ToString() == dt.Rows[a - 1]["Column2"].ToString() && dt.Rows[a]["Column3"].ToString() == dt.Rows[a - 1]["Column3"].ToString() && dt.Rows[a]["Column4"].ToString() == dt.Rows[a - 1]["Column4"].ToString())
            //{
            //    dt.Rows[a].Delete();
            //}
            //if (dt.Rows[o]["Column1"].ToString() == dt.Rows[o - 1]["Column1"].ToString() && dt.Rows[o]["Column2"].ToString() == dt.Rows[o - 1]["Column2"].ToString() && dt.Rows[o]["Column3"].ToString() == dt.Rows[o - 1]["Column3"].ToString() && dt.Rows[o]["Column4"].ToString() == dt.Rows[o - 1]["Column4"].ToString())
            //{
            //    dt.Rows[0].Delete();
            //}
            GridviewCoupon.DataBind();
            if (dt.Rows.Count > 0)
            {
                int j = dt.Rows.Count;

                ViewState["dt_CouponData"] = dt;


                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    TextBox box1 = (TextBox)GridviewCoupon.Rows[i].Cells[1].FindControl("TextBox1");
                    TextBox box2 = (TextBox)GridviewCoupon.Rows[i].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)GridviewCoupon.Rows[i].Cells[3].FindControl("TextBox3");
                    TextBox box4 = (TextBox)GridviewCoupon.Rows[i].Cells[4].FindControl("TextBox4");

                    box1.Text = dt.Rows[i]["Column1"].ToString();
                    box2.Text = dt.Rows[i]["Column2"].ToString();
                    box3.Text = dt.Rows[i]["Column3"].ToString();
                    box4.Text = dt.Rows[i]["Column4"].ToString();

                    //if (rowIndex<dt.Rows.Count-1)
                    //{
                    //    rowIndex++;
                    //}

                }
            }
        }
    }

    protected void GridviewCoupon_RowCommand(object sender, GridViewCommandEventArgs e)
    {


        if (e.CommandName == "More")
        {
            objENT = new ENT();
            DataTable dt = (DataTable)ViewState["dt_CouponData"];

            int index = Convert.ToInt32(e.CommandArgument.ToString());
            if (dt.Rows[0]["Column1"].ToString() == "" && dt.Rows[0]["Column2"].ToString() == "" && dt.Rows[0]["Column3"].ToString() == "" && dt.Rows[0]["Column4"].ToString() == "")
            {
                index++;

            }
            Save();
            int count = dt.Rows.Count;
            if (count == index)
            {
                dt.Rows[index - 1].Delete();
            }
            else
            {
                dt.Rows[index].Delete();
            }
            ViewState["dt_CouponData"] = dt;

            SetPrevious();


        }
    }


    protected void GridviewCoupon_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button Delete = (Button)e.Row.FindControl("btnDelete");
            DataRowView dr = e.Row.DataItem as DataRowView;
            int index = e.Row.RowIndex;
            if (index == 0)
            {
                Delete.Visible = false;
            }
            else
            {
                Delete.Visible = true;
            }

        }

    }
    #endregion
<div class="row" style="margin-top: 2%">
                        <div class="col-sm-1"></div>
                        <div class="container-fluid col-sm-10">

                            <asp:GridView ID="GridviewCoupon" OnRowEditing="GridviewCoupon_RowEditing" OnRowDataBound="GridviewCoupon_RowDataBound" OnRowCommand="GridviewCoupon_RowCommand" runat="server" Enabled="false" Style="text-align: center" ShowFooter="true" Width="99%"
                                AutoGenerateColumns="false">
                                <Columns>
                                    <%--<asp:BoundField DataField="RowNumber" HeaderText="Sr No" HeaderStyle-BackColor="#99CCCC" />--%>
                                    <%--   <asp:TemplateField HeaderText="Sr No" HeaderStyle-BackColor="#99CCCC">
                                        <ItemTemplate>
                                            <asp:Label ID="txtbox0" Text='<%# Bind("RowNumber")%>' runat="server"></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>--%>
                                    <%--<asp:TemplateField HeaderText="Survey Number" HeaderStyle-BackColor="#99CCCC">
                                        <ItemTemplate>
                                            <asp:TextBox ID="TextBoxS" CssClass="form-control" required="required" runat="server"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>--%>
                                    <asp:TemplateField HeaderText="Coupon Code" HeaderStyle-BackColor="#99CCCC">
                                        <ItemTemplate>
                                            <asp:TextBox ID="TextBox1" required="required" CssClass="form-control" runat="server"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderText="Coupon Description" HeaderStyle-BackColor="#99CCCC">
                                        <ItemTemplate>
                                            <asp:TextBox ID="TextBox2" MaxLength="250" required="required" CssClass="form-control" runat="server"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>

                                    <asp:TemplateField HeaderText="Start Date" HeaderStyle-BackColor="#99CCCC">
                                        <ItemTemplate>
                                            <asp:TextBox ID="TextBox3"  required="required" placeholder="DD/MMM/YYYY" CssClass="form-control cal" runat="server"></asp:TextBox>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="End Date" HeaderStyle-BackColor="#99CCCC">
                                        <ItemTemplate>
                                            <asp:TextBox ID="TextBox4"  required="required" placeholder="DD/MM/YYYY" CssClass="form-control" runat="server"></asp:TextBox>
                                        </ItemTemplate>

                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="End Date" HeaderStyle-BackColor="#99CCCC">
                                        <ItemTemplate>
                                            <asp:Button ID="btnDelete"  Text="Delete"
                                                CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
                                                CommandName="More" CssClass="btn-primary btn"
                                                Style="padding-top: 1%; padding-bottom: 1%; margin-top: 1px; margin-bottom: 1px" runat="server" />
                                        </ItemTemplate>


                                        <FooterStyle HorizontalAlign="Right" />
                                        <FooterTemplate>
                                            <asp:Button ID="ButtonAdd" Font-Bold="true" runat="server" Text="Add New Row" CssClass="btn-toolbar btn" OnClick="ButtonAdd_Click" />
                                        </FooterTemplate>
                                    </asp:TemplateField>
                                </Columns>

                                <%--    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />--%>
                                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                                <RowStyle BackColor="White" ForeColor="#003399" />
                                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                                <SortedAscendingCellStyle BackColor="#EDF6F6" />
                                <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                                <SortedDescendingCellStyle BackColor="#D6DFDF" />
                                <SortedDescendingHeaderStyle BackColor="#002876" />
                            </asp:GridView>

                    </div>

here is the whole code came through but took me like 1-2 days:) hope it helps

这篇关于如何为GridView中的特定行删除行或禁用删除按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆