如何从sql数据库中获取数据并将它们绑定在网格视图按钮中 [英] how to fetch data from sql database and bind them in grid view button

查看:93
本文介绍了如何从sql数据库中获取数据并将它们绑定在网格视图按钮中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Collections;

使用System.Configuration;

使用System.Data;

使用System.Linq;

使用System.Web;

使用System.Web.Security;

使用System.Web.UI ;

使用System.Web.UI.HtmlControls;

使用System.Web.UI.WebControls;

使用System.Web.UI。 WebControls.WebParts;

使用System.Xml.Linq;

使用System.Data.SqlClient;



public partial class StudentAttendanceReport:System.Web.UI.Page

{

GeneralUtility OGu;

static int totalstudents;

string Sql;

GeneralUtility oGU;

DataTable oDt;

protected void Page_Load(object sender,EventArgs e)

{

if(Session [userID] == null)

{

postAtParent();

}

其他

{
totalstudent();

if(!IsPostBack)

{

string RoleID = Session [roleID] .ToString();

if(RoleID ==R00001)

{

SchoolBind();

lblMsg.Text =;

trIn.Visible = false;



ddlbind();

}

else

{

orgrow.Visible = false;

SchoolBind();

lblMsg.Text =;

trIn.Visible = false;



ddlbind();

ddlclassbind();



}

}

}

}

public void postAtParent()

{

string str =;

str =< script launguage = Javascript>;

str = str +document.forms [0] .target ='_ top';;

str = str +document.forms [0] .action = 'loginform.aspx';; //网址到这里

str = str +document.forms [0] .submit();;

str = str +< / script> ;

Page.ClientScript.RegisterClientScriptBlock(Page.GetType(),assignToparent,str);

}

public void ddlclassbind()

{

oGU = new GeneralUtility();

oDt = new DataTable();

Sql = null ;

string OrgID = Session [Org]。ToString();

if(Session [roleID]。ToString()==R00001)

{

Sql =从ACC_APP1_Student选择DISTINCT类,其中OrgID ='+ ddlOrg.SelectedValue.ToString()+'ORDER BY CLASS;

}

其他

{

Sql =从ACC_APP1_Student中选择DISTINCT类,其中OrgID ='+ OrgID +'ORDER BY CLASS;

}

oDt = oGU.GetDataTable(Sql);

if(oDt.Rows.Count> 0)

{

ddlClass.DataSource = oDt;

ddlClass.DataTextField =Class;

/ /ddlClass.DataValueField =Class;

ddlClass.DataBind();

ddlClass.Items.Insert(0,ALL);

}

}

public void ddlbind()

{

oGU = new GeneralUtility();

oDt = new DataTable();

Sql =通过OrgName从OrganizationDetail订单中选择*;

oDt = oGU.GetDataTable(Sql) ;

ddlOrg.DataSource = oDt;

ddlOrg.DataTextField =OrgName;

ddlOrg.DataValueField =OrgID;

ddlOrg.DataBind();

ddlOrg.Items.Insert(0,Select);

}

public void totalstudent()

{

oGU = new GeneralUtility();

oDt = new DataTable( ); $



string ddclass = ddlClass.SelectedValue.ToString();

string RoleID = Session [roleID]。ToString() ;

if(RoleID ==R00001)

{

string ddorg = ddlOrg.SelectedValue.ToString();

Sql = null;

if(ddclass ==ALL)

Sql =select * from dbo.ACC_APP1_Student where orgid ='+ ddorg + ';

其他

Sql =select * from dbo.ACC_APP1_Student where orgid ='+ ddorg +'and class ='+ ddclass +' ;

oDt = oGU.GetDataTable(Sql);

totalstudents = oDt.Rows.Count;



}

else

{

string OrgID = Session [Org]。ToString();

Sql = null;

if(ddclass ==ALL)

Sql = select * from dbo.ACC_APP1_Student where orgid ='+ orgID +';

else

Sql =select * from dbo.ACC_APP1_Student where orgid =' + OrgID +'和class ='+ ddclass +';

oDt = oGU.GetDataTable(Sql);

totalstudents = oDt.Rows.Count;

}

}

public void SchoolBind()

{

// reqId = Session [Org]。ToString();



oGU = new GeneralUtility();

oDt = new DataTable() ; $ / b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
if(RoleID ==R00001)

{

ddlOrg.Visible = true;

Sql =select OrgId,Orgname来自OrganizationDetail;

oDt = oGU.GetDataTable(Sql);

ddlOrg.Data Source = oDt;

ddlOrg.DataValueField =OrgId;

ddlOrg.DataTextField =Orgname;

ddlOrg.DataBind();

ddlOrg.Items.Insert(0,Select);

}

else

{

// ddlOrg.Visible = false;

Sql =select OrgId,Orgname from OrganizationDetail OrgID ='+ OrgID +';

oDt = oGU.GetDataTable(Sql);

ddlOrg.DataSource = oDt;

ddlOrg.DataValueField =OrgId;

ddlOrg。 DataTextField =Orgname;

ddlOrg.DataBind();

ddlOrg.Items.Insert(0,Select);

}



}

int GetDaysInMonth(int month,int year)

{

如果(月< 1 ||月份> 12)

{

抛出新的System.ArgumentOutOfRangeException(月,月,月必须在1到12之间);

}

if(1 == month || 3 == month || 5 == month || 7 == month || 8 == month ||

10 = =月|| 12 ==月)

{

返回31;

}

else if(2 ==月)

{



if(0 ==(年%4))

{



if(0 ==(年%400))

{

返回29;

}

否则如果(0 ==(年%100))

{

返回28;

}





返回29;

}



返回28;

}

返回30;

}

public void monthbind()

{

int days = GetDaysInMonth(Convert.ToInt32(ddlmonth.SelectedValue),Convert.ToInt32(DateTime.Now.Year));

DataTable myDT = new DataTable();

for(int i = 0;我<天; i ++)

{

DataColumn id = new DataColumn(id);

id.ColumnName = id + i.ToString();

id.DataType = System.Type.GetType(System.Int32);

myDT.Columns.Add(id);

}



for(int i = 1; i< = 1; i ++)

{

DataRow dr = myDT.NewRow();

for(int ii = 0; ii< days; ii ++)

{

dr [ii ] = ii.ToString();

}

myDT.Rows.Add(dr);

}

GridView1.DataSource = myDT;

GridView1.DataBind();

GridView1.Visible = true;

}



public void fillgrid()

{

GeneralUtility ogu = new GeneralUtility();

DataTable oDt = new DataTable();

SqlCommand cmd = ne w SqlCommand(选择StudentID,来自ACC_APP1_Student的名字);

cmd.CommandType = CommandType.Text;





}

protected void ddlmonth_SelectedIndexChanged(object sender,EventArgs e)

{

monthbind();

}

protected void ddlOrg_SelectedIndexChanged(object sender,EventArgs e)

{

ddlclassbind();

}

protected void ddlClass_SelectedIndexChanged(object sender,EventArgs e)

{

ddlbind();

}

protected void btnInReport_Click(object sender,EventArgs e)

{

monthbind();

}

protected void btnImportToExcel_Click(object sender,EventArgs e)

{



}

protected void GridView1_SelectedIndexChanged(对象发送者,EventArgs e)

{



}

}

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;
using System.Data.SqlClient;

public partial class StudentAttendanceReport : System.Web.UI.Page
{
GeneralUtility OGu;
static int totalstudents;
string Sql;
GeneralUtility oGU;
DataTable oDt;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userID"] == null)
{
postAtParent();
}
else
{
totalstudent();
if (!IsPostBack)
{
string RoleID = Session["roleID"].ToString();
if (RoleID == "R00001")
{
SchoolBind();
lblMsg.Text = "";
trIn.Visible = false;

ddlbind();
}
else
{
orgrow.Visible = false;
SchoolBind();
lblMsg.Text = "";
trIn.Visible = false;

ddlbind();
ddlclassbind();

}
}
}
}
public void postAtParent()
{
string str = "";
str = "<script launguage=Javascript>";
str = str + "document.forms[0].target='_top';";
str = str + "document.forms[0].action='loginform.aspx';"; // url goes here
str = str + "document.forms[0].submit();";
str = str + "</script>";
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "assignToparent", str);
}
public void ddlclassbind()
{
oGU = new GeneralUtility();
oDt = new DataTable();
Sql = null;
string OrgID = Session["Org"].ToString();
if (Session["roleID"].ToString() == "R00001")
{
Sql = "Select DISTINCT Class From ACC_APP1_Student Where OrgID='" + ddlOrg.SelectedValue.ToString() + "' ORDER BY CLASS";
}
else
{
Sql = "Select DISTINCT Class From ACC_APP1_Student Where OrgID='" + OrgID + "' ORDER BY CLASS";
}
oDt = oGU.GetDataTable(Sql);
if (oDt.Rows.Count > 0)
{
ddlClass.DataSource = oDt;
ddlClass.DataTextField = "Class";
//ddlClass.DataValueField = "Class";
ddlClass.DataBind();
ddlClass.Items.Insert(0, "ALL");
}
}
public void ddlbind()
{
oGU = new GeneralUtility();
oDt = new DataTable();
Sql = "select * from OrganizationDetail order by OrgName";
oDt = oGU.GetDataTable(Sql);
ddlOrg.DataSource = oDt;
ddlOrg.DataTextField = "OrgName";
ddlOrg.DataValueField = "OrgID";
ddlOrg.DataBind();
ddlOrg.Items.Insert(0, "Select");
}
public void totalstudent()
{
oGU = new GeneralUtility();
oDt = new DataTable();

string ddclass = ddlClass.SelectedValue.ToString();
string RoleID = Session["roleID"].ToString();
if (RoleID == "R00001")
{
string ddorg = ddlOrg.SelectedValue.ToString();
Sql = null;
if (ddclass == "ALL")
Sql = " select * from dbo.ACC_APP1_Student where orgid='" + ddorg + "' ";
else
Sql = " select * from dbo.ACC_APP1_Student where orgid='" + ddorg + "' and class='" + ddclass + "' ";
oDt = oGU.GetDataTable(Sql);
totalstudents = oDt.Rows.Count;

}
else
{
string OrgID = Session["Org"].ToString();
Sql = null;
if (ddclass == "ALL")
Sql = " select * from dbo.ACC_APP1_Student where orgid='" + OrgID + "' ";
else
Sql = " select * from dbo.ACC_APP1_Student where orgid='" + OrgID + "' and class='" + ddclass + "' ";
oDt = oGU.GetDataTable(Sql);
totalstudents = oDt.Rows.Count;
}
}
public void SchoolBind()
{
// reqId = Session["Org"].ToString();

oGU = new GeneralUtility();
oDt = new DataTable();
string OrgID = Session["Org"].ToString();
string RoleID = Session["roleID"].ToString();
if (RoleID == "R00001")
{
ddlOrg.Visible = true;
Sql = "select OrgId, Orgname from OrganizationDetail ";
oDt = oGU.GetDataTable(Sql);
ddlOrg.DataSource = oDt;
ddlOrg.DataValueField = "OrgId";
ddlOrg.DataTextField = "Orgname";
ddlOrg.DataBind();
ddlOrg.Items.Insert(0, "Select");
}
else
{
// ddlOrg.Visible = false;
Sql = "select OrgId, Orgname from OrganizationDetail Where OrgID='" + OrgID + "'";
oDt = oGU.GetDataTable(Sql);
ddlOrg.DataSource = oDt;
ddlOrg.DataValueField = "OrgId";
ddlOrg.DataTextField = "Orgname";
ddlOrg.DataBind();
ddlOrg.Items.Insert(0, "Select");
}

}
int GetDaysInMonth(int month, int year)
{
if (month < 1 || month > 12)
{
throw new System.ArgumentOutOfRangeException("month", month, "month mustbe between 1 and 12");
}
if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month ||
10 == month || 12 == month)
{
return 31;
}
else if (2 == month)
{

if (0 == (year % 4))
{

if (0 == (year % 400))
{
return 29;
}
else if (0 == (year % 100))
{
return 28;
}


return 29;
}

return 28;
}
return 30;
}
public void monthbind()
{
int days = GetDaysInMonth(Convert.ToInt32(ddlmonth.SelectedValue), Convert.ToInt32(DateTime.Now.Year));
DataTable myDT = new DataTable();
for (int i = 0; i < days; i++)
{
DataColumn id = new DataColumn("id");
id.ColumnName = id + i.ToString();
id.DataType = System.Type.GetType("System.Int32");
myDT.Columns.Add(id);
}

for (int i = 1; i <= 1; i++)
{
DataRow dr = myDT.NewRow();
for (int ii = 0; ii < days; ii++)
{
dr[ii] = ii.ToString();
}
myDT.Rows.Add(dr);
}
GridView1.DataSource = myDT;
GridView1.DataBind();
GridView1.Visible = true;
}

public void fillgrid()
{
GeneralUtility ogu=new GeneralUtility();
DataTable oDt = new DataTable();
SqlCommand cmd = new SqlCommand("select StudentID, Name from ACC_APP1_Student");
cmd.CommandType = CommandType.Text;


}
protected void ddlmonth_SelectedIndexChanged(object sender, EventArgs e)
{
monthbind();
}
protected void ddlOrg_SelectedIndexChanged(object sender, EventArgs e)
{
ddlclassbind();
}
protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
{
ddlbind();
}
protected void btnInReport_Click(object sender, EventArgs e)
{
monthbind();
}
protected void btnImportToExcel_Click(object sender, EventArgs e)
{

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
}

推荐答案

您好,

如果您正在寻找来自sql的绑定网格,请阅读这篇文章。

如何使用C#中的SQL语句填充DataGridView,GridView [ ^ ]



您将从中获得大量解决方案谷歌。所有你要做的就是阅读并试试。



祝你好运
Hi,
If you are looking for binding grid from sql, read this article.
How to populate DataGridView, GridView with SQL statement in C#[^]

you will get plenty of solutions from google. All you to do is read and try it.

Good Luck


这篇关于如何从sql数据库中获取数据并将它们绑定在网格视图按钮中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆