我有一个将AutoGenerateColumns设置为FALSE的gridview。我手动添加了列(BoundField和ButtonField)以确保按钮显示在最后一列,因为如果AutoGenerateColumns设置为TRUE,则添加的ButtonField的默认位置位于第一列。
如果将AutoGenerateColumns设置为TRUE,则以下代码可以正常工作。但是,如果我将其更改为false并添加BoundFields和ButtonField,则它不起作用。查询工作正常,如果AutoGenerateColumns设置为TRUE,我已经看到Gridview填充没有问题。
请协助解决问题。我希望按钮位于列的右侧。因此,下面的aspx设计。
代码:
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" Width="100%" EmptyDataText="No records to display." AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="COL1" />
<asp:BoundField HeaderText="COL2" />
<asp:BoundField HeaderText="COL3" />
<asp:BoundField HeaderText="COL4" />
<asp:BoundField HeaderText="COL5" />
<asp:ButtonField ButtonType="Button" Text="Update"/>
</Columns>
........
</asp:GridView>
DataTable GetData()
{
DataTable dt = new DataTable();
string LoginDbCS = ConfigurationManager.ConnectionStrings["UserDetails"].ConnectionString;
SqlConnection Conn = new SqlConnection(LoginDbCS);
SqlCommand Command = new SqlCommand("SELECT [COL1], [COL2], [COL3], [COL4], [COL5] FROM dbo.tableUser WHERE @User = Username", Conn);
try
{
Conn.Open();
Command.Parameters.Add("User", SqlDbType.NVarChar).Value = Username;
SqlDataAdapter adpt = new SqlDataAdapter(Command);
adpt.Fill(dt);
Conn.Close();
}
catch (Exception Er)
{
....
if (Conn != null)
Conn.Dispose();
}
....
}
return dt;
}
protected void Page_Load(object sender, EventArgs e)
{
....
GridView1.DataSource = GetData();
GridView1.DataBind();
....
}
答案 0 :(得分:0)
试试这个
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" Width="100%" EmptyDataText="No records to display." AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="COL1" HeaderText="COL1" />
<asp:BoundField DataField="COL2" HeaderText="COL2" />
<asp:BoundField DataField="COL3" HeaderText="COL3" />
<asp:BoundField DataField="COL4" HeaderText="COL4" />
<asp:BoundField DataField="COL5" HeaderText="COL5" />
<asp:CommandField ShowSelectButton="True" SelectText="Update"/>
</Columns>
........
</asp:GridView>
然后为SelectedIndexChanged创建一个事件来处理选定的行