我想在gridview中显示我在运行时插入的所有数据。为此,我编写了代码,但收到此错误。
“DataSource和DataSourceID都是 在'GridView1'上定义。删除一个 定义“。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
string str = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString();
SqlConnection con = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select * from Items where ItemId='" + TxtItemId.Text + "'", con);
DataSet ds = new DataSet();
da.Fill(ds,"Items");
GridView1.DataSource = ds;
GridView1.DataBind();
}
请修改我的代码,这是我的错误。
谢谢, 萨米特
答案 0 :(得分:1)
这两个属性是互斥的(当你使用其中一个属性时,不允许使用另一个属性):
示例:
的DataSourceID
@asp:ObjectDataSource ID =“ods1”runat =“server”SelectMethod =“Test”TypeName =“TestBL”/ @ @asp:GridView ID =“gv1”runat =“server”DataSourceID =“ods1”/ @
在这种情况下,数据绑定自动发生
数据源
@asp:GridView ID =“gv2”runat =“server”%@
在代码背后你会有这样的东西:
overrides void OnLoad(..)
{
List<DataObject> source = new List<DataObject>();
source.Add(new DataObject(..));
source.Add(new DataObject(..));
source.Add(new DataObject(..));
gv2.DataSource = source;
gv2.DataBind();
}
请注意我使用了@而不是尖括号。
答案 1 :(得分:0)
您似乎想要手动绑定数据源。从gridview中删除设计视图中的DataSourceID属性。它应该是这样的:
<asp:GridView runat="server" ID="grid" DataSourceID="SqlDataSource1"></asp:GridView>
只需删除DataSourceID =“SqlDataSource1”属性。
答案 2 :(得分:0)
您需要使用SqlParameter和sql命令。
你缺少逻辑。首先,当表单加载时,你需要将完整的Item Table数据绑定到Grid,Ten你需要一个文本框和按钮来过滤网格。
这是您的完整代码。用它。 100%有效。
<div id = "dvFilterGrid">
Filter Grid : <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox>
<asp:Button ID="btnFilterGrid" runat="server" Text="Find"
onclick="btnFilterGrid_Click" />
</div>
以及您的代码
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridData();
}
}
protected void BindGridData()
{
con.Open();
using (SqlCommand cmd = new SqlCommand("Select * from Items", con))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void btnFilterGrid_Click(object sender, EventArgs e)
{
int ItemId = Convert.ToInt32(txtFilter.Text.Trim());
FilterData(ItemId);
}
private void FilterData(int ItemId)
{
string query = "select * from Items where ItemId=@ItemId ";
SqlCommand cmd = new SqlCommand(query,con);
cmd.Parameters.Add(new SqlParameter("@ItemId", ItemId));
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"Items");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
答案 3 :(得分:0)
用于在按钮上插入数据
SqlConnection con = new SqlConnection("connection string");
SqlDataAdapter myadp = new SqlDataAdapter();
myadp.InsertCommand = new SqlCommand();
myadp.InsertCommand.Connection = con;
con.Open();
myadp.InsertCommand.CommandText = "insert into number values(@Silver)";
myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
int i = myadp.InsertCommand.ExecuteNonQuery();
if (i == 1)
{
// Response.Write("<script>alert('number updated sucessfully')</script>");
Label2.Text = "Number updated sucessfully".ToString();
TextBox10.Text = "";
}
else
{
//Response.Write("<script>alert('somthing went wrong try again')</script>");
Label2.Text = "somthing went wrong try again".ToString();
}
con.Close();
Responce.Redirect("your page.aspx");
在网格视图上显示数据
SqlCommand command = new SqlCommand("SELECT * from number");
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
所以整体编码将是
SqlConnection con = new SqlConnection("connection string");
SqlDataAdapter myadp = new SqlDataAdapter();
myadp.InsertCommand = new SqlCommand();
myadp.InsertCommand.Connection = con;
con.Open();
myadp.InsertCommand.CommandText = "insert into number values(@Silver)";
myadp.InsertCommand.Parameters.AddWithValue("@Silver", TextBox10.Text);
int i = myadp.InsertCommand.ExecuteNonQuery();
if (i == 1)
{
// Response.Write("<script>alert('number updated sucessfully')</script>");
Label2.Text = "Number updated sucessfully".ToString();
TextBox10.Text = "";
}
else
{
//Response.Write("<script>alert('somthing went wrong try again')</script>");
Label2.Text = "somthing went wrong try again".ToString();
}
con.Close();
Responce.Redirect("your page.aspx");
To display data on the grid view
SqlCommand command = new SqlCommand("SELECT * from number");
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
here the Responce.Redirect("your page.aspx"); will do the trick for you