ASP.NET网格视图数据无法显示 [英] ASP.NET grid view data can not show
问题描述
info.aspx.cs
info.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class info : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
/*String name;
String address;
String department;
if (gender.SelectedItem.Value == "Male")
{
name = nameTextBox.Text.Trim();
address = addressTextBox.Text.Trim();
department=showDropDownList.Text.Trim();
String query="insert into info (Name,Address,Gender,Department) values ('" + name + "','" + address + "','Male','" + department + "')";
DataAccess.ExecuteSQL(query);
}
else if (gender.SelectedItem.Value == "Female")
{
name = nameTextBox.Text.Trim();
address = addressTextBox.Text.Trim();
department=showDropDownList.Text.Trim();
String query = "insert into info (Name,Address,Gender,Department) values ('" + name + "','" + address + "','Female','" + department + "')";
DataAccess.ExecuteSQL(query);
}*/
String sql = "SELECT Name, Address, Gender, Department FROM info ";
DataTable t = DataAccess.GetDataTable(sql);
this.GridView1.Visible = true;
GridView1.DataSource = t;
GridView1.DataBind();
/*nameTextBox.Text = "";
addressTextBox.Text = "";
gender.ClearSelection(); */
}
}
}
info.aspx
info.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="info.aspx.cs" Inherits="WebApplication1.info" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<asp:Label ID="nameLabel" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="nameTextBox" runat="server" Height="28px"
style="margin-left: 81px" Width="221px"></asp:TextBox>
<br />
<br />
<asp:Label ID="addressLabel" runat="server" Text="Address"></asp:Label>
<asp:TextBox ID="addressTextBox" runat="server" style="margin-left: 66px"
Width="225px" Height="28px" ></asp:TextBox>
<br />
<br />
<table style="width:100%;">
<tr>
<td><asp:Label ID="genderLabel" runat="server" Text="Gender"></asp:Label> </td>
<td> <asp:RadioButtonList ID="gender" runat="server" Height="24px"
Width="190px" style="margin-left: 81px; margin-top: 0px;">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList></td>
</tr>
</table>
<br />
<asp:Label ID="deptLabel" runat="server" Text="Department"></asp:Label>
<asp:DropDownList ID="showDropDownList" runat="server">
<asp:ListItem>BBA</asp:ListItem>
<asp:ListItem>CSE</asp:ListItem>
<asp:ListItem>EEE</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Show" />
<br />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
style="margin-top: 0px" Width="529px">
</asp:GridView>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</div>
</form>
</body>
</html>
推荐答案
更改gridview属性
设置此属性
AutoGenerateColumns =True
change your gridview property
set this property
AutoGenerateColumns="True"
Hii ,,,朋友。
可能是你得到了一些帮助...
有两种方式来解决这个问题...
(1)使用此gridview:
Hii,,, Friend.
may be you get some help from this...
There are two ways to solve this problem...
(1) use this gridview :
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
style="margin-top: 0px" Width="529px">
<Columns>
<asp:BoundField DataField ="Name" HeaderText ="Name" ReadOnly="true" />
<asp:BoundField DataField ="Address" HeaderText ="Address" ReadOnly="true"/>
<asp:BoundField DataField ="Gender " HeaderText ="Gender " ReadOnly="true"/>
<asp:BoundField DataField ="Department" HeaderText ="Department" ReadOnly="true"/>
</Columns>
</asp:GridView>
(2)Plz在你的代码中改变它 AutoGenerateColumns =False,到
AutoGenerateColumns =True ...
然后你在网格视图中得到数据....
喜欢这个..
(2) Plz change this in your code AutoGenerateColumns="False", to
AutoGenerateColumns="True"...
then you get the data in the grid view....
like this..
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
style="margin-top: 0px" Width="529px">
</asp:GridView>
Also keep in mind you have to Bind the DataSource to GridView in Code Behind.......
also you Bind the data in the Page Load..
Hopefully it Works..
Best of Luck...
Also keep in mind you have to Bind the DataSource to GridView in Code Behind.......
also you Bind the data in the Page Load..
Hopefully it Works..
Best of Luck...
use this grid view
use this grid view
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False"
Width="100%">
<Columns>
<asp:BoundField DataField ="Name" HeaderText ="Name" ReadOnly="true" />
<asp:BoundField DataField ="Address" HeaderText ="Address" ReadOnly="true"/>
<asp:BoundField DataField ="Gender " HeaderText ="Gender " ReadOnly="true"/>
<asp:BoundField DataField ="Department" HeaderText ="Department" ReadOnly="true"/>
</Columns>
</asp:GridView>
OR
set autogenratedcolumn =true in your code
OR
set autogenratedcolumn =true in your code
这篇关于ASP.NET网格视图数据无法显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!