如何将GridView中的列设置为默认值?

时间:2015-07-21 13:31:01

标签: c# asp.net gridview default-value

我在GridView中有一个名为Qty Advised的列,此列需要默认为1.但每次加载GridView时,它都会在列中显示0。我将数据库中的默认值设置为1并在SerNo.QtyAdvised = 1;函数中设置ItemDataBound,但它仍然显示为0.

有没有办法在这里设置默认值?

<asp:BoundField DataField="QtyAdvised" HeaderText="Qty Advised" ></asp:BoundField>

我不明白它如何显示0 GridView是动态创建的。

2 个答案:

答案 0 :(得分:3)

您可能需要使用DataColumn.DefaultValue财产。

它在您创建新行时设置列的默认值。

参考:DataColumn.DefaultValue Property

或者在rowdatabound上,您可以设置列默认值

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //Checking the RowType of the Row  
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         e.Row.Cells[1].Text = "1"; // Use your column index here
    }  
}

答案 1 :(得分:0)

它的工作原理是在数据库的代码中设置默认值

public int _QtyAdvised = 1;
public int QtyAdvised
        {
            get { return _QtyAdvised; }
            set { _QtyAdvised = value; }
        }