本文介绍了我正在使用网格视图在数据库中加入两个表。代码好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
加入gridview。
for joining gridview.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace country
{
public partial class join_gridview : System.Web.UI.Page
{
getvalue obj = new getvalue();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GetRecord();
Getdata1();
}
else
{
}
}
public void GetRecord()
{
string sql = string.Format("SELECT dbo.city.Id AS ID, dbo.city.cityname, dbo.state.StateName FROM dbo.state INNER JOIN dbo.city ON dbo.state.Id = dbo.city.stateid");
DataSet ds = new DataSet();
ds = obj.getdata(sql);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
else
{
}
}
public void Getdata1()
{
string sql = string.Format("select * from state");
DataSet ds = new DataSet();
ds = obj.getdata(sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpstate.DataSource = ds;
drpstate.DataTextField = "StateName";
drpstate.DataValueField = "id";
drpstate.DataBind();
}
else
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Panel2.Visible = true;
Panel1.Visible = false;
Label1.Visible = false;
drpstate.Visible = false;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "linkedit")
{
Panel2.Visible = true;
Panel1.Visible = false;
Button2.Visible = false;
Button3.Visible = false;
string sql = string.Format("select * from city where ID={0}", e.CommandArgument);
DataSet ds= new DataSet();
ds = obj.getdata(sql);
if (ds.Tables[0].Rows.Count > 0)
{
drpstate.SelectedValue = ds.Tables[0].Rows[0]["stateid"].ToString();
TextBox2.Text = ds.Tables[0].Rows[0]["cityname"].ToString();
//// TextBox3.Text = ds.Tables[0].Rows[0]["cityname"].ToString();
hidenfield.Value = ds.Tables[0].Rows[0]["ID"].ToString();
}
}
else
{
string sql1 = string.Format("delete from city where ID={0}", e.CommandArgument);
obj.updatedata(sql1);
GetRecord();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
string sql = string.Format("insert into city(cityname,StateID)values('{0}',{1})", TextBox3.Text, drpstate.SelectedValue);
obj.updatedata(sql);
GetRecord();
}
protected void Button4_Click(object sender, EventArgs e)
{
string sql = string.Format("update city set cityname='{0}',StateID={1} where ID={2}", TextBox2.Text, drpstate.SelectedValue, hidenfield.Value);
obj.updatedata(sql);
GetRecord();
Panel1.Visible = true;
Panel2.Visible = false;
}
protected void Button5_Click(object sender, EventArgs e)
{
Panel1.Visible = true;
Panel2.Visible = false;
}
}
}
为班级
for the class
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace country
{
public class GET
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString);
public DataSet getdata(string sql)
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter adap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
con.Close();
return ds;
}
public void update(string sql)
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
gridview aspx页面
gridview aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="join gridview.aspx.cs" Inherits="country.join_gridview" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand" >
<Columns>
<asp:BoundField DataField="StateName" HeaderText="State Name" />
<asp:BoundField DataField="cityname" HeaderText="City" />
<asp:TemplateField HeaderText="EDIT">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%#Eval("ID") %>' CommandName="linkedit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DELETE">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server"
CommandArgument='<%#Eval("ID") %>' CommandName="linkdelete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="ADD" onclick="Button1_Click" />
</asp:Panel>
</div>
<asp:Panel ID="Panel2" runat="server" Visible="False">
<br />
<span lang="en-us">
<asp:Label ID="Label1" runat="server" Text="Name of the state:"></asp:Label></span>
<asp:DropDownList ID="drpstate" runat="server">
</asp:DropDownList>
<br />
<br />
<span lang="en-us">Name: </span>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
<span lang="en-us">Name of the Cty:</span><asp:TextBox ID="TextBox2"
runat="server"></asp:TextBox>
<br />
<br />
<br />
<span lang="en-us"> </span>
<asp:Button ID="Button2" runat="server" Text="ADD" onclick="Button2_Click" />
<span lang="en-us"> </span>
<asp:Button ID="Button3" runat="server" Text="RESET" Visible="False" />
<span lang="en-us"> </span>
<asp:Button ID="Button4" runat="server" Text="Update"
onclick="Button4_Click" />
<span lang="en-us"> </span>
<asp:Button ID="Button5" runat="server" Text="Cancel" onclick="Button5_Click" />
<span lang="en-us"> </span>
</asp:Panel>
<asp:HiddenField ID="hidenfield" runat="server" />
</form>
</body>
</html>
is the code ok?
is the code ok?
推荐答案
这篇关于我正在使用网格视图在数据库中加入两个表。代码好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!