在gridview中显示数据 [英] To Show the data in gridview

查看:57
本文介绍了在gridview中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生,

我正在使用像Employee&Department这样的两个sql表。

1)在Employee表中,字段是Empid,Empname,Deptid,Salary等。

2)在Department表中,字段是Deptid,Departmentname,Deduction_amount等。

3)我们在部门表中手动插入记录。

4)我创建网页employeedetails.aspx

并在员工表中写入插入记录的代码。

5)我在网格视图列中显示表员工和部门的所有上述字段然后

我想将另一列添加到gridview,以便从部门表中从员工表减去deduction_amount的工资计算netsalary。



我接受了gridview字段,如

Empname,Departmentname,Salary,Deduction_amount,Net Salary(Salary-deduction_amount)



请给我正确的解决方案。



谢谢,

Respected Sir,
I am using the two sql table like Employee & Department.
1)In Employee table fields are Empid,Empname,Deptid,Salary etc.
2)In Department table fields are Deptid,Departmentname,Deduction_amount etc.
3)We insert record manually in department table.
4)I am create the web page "employeedetails.aspx"
& write the code for insert record in employee table.
5)I am displaying all above field of table employee and department in grid view column then
I want to add one another column to gridview for netsalary calculating from salary of employee table subtract deduction_amount from department table.

I am taken the gridview field like
Empname,Departmentname,Salary,Deduction_amount,Net Salary(Salary-deduction_amount)

Please given me the proper solution.

Thanks,

推荐答案

嗨朋友.......



你必须在aspx页面中制作gridView这样的

有两种方式获取数据的Gridview:



(1)仅对于AutoGenerateColumns列为gridView的属性

hi friend.......

frist you have to make gridView in the aspx page like this
there are two ways of Gridview for the getting the Data:

(1)only true the property of gridView for the column which is AutoGenerateColumns
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True"
            style="margin-top: 0px" Width="529px">
        </asp:GridView>







(2)让你的列像你一样这但设置AutoGenerateColumns =False...






(2)make Column inyour way like this but set the AutoGenerateColumns="False"...

<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" Style="margin-top: 0px"
    Width="529px" EmptyDataText="No data availble..">
    <Columns>
        <asp:BoundField DataField="EmpName" HeaderText="Employee Name" ReadOnly="true" />
        <asp:BoundField DataField="DepartmentName" HeaderText="Department Name" ReadOnly="true" />
        <asp:BoundField DataField="Salary" HeaderText="Salary" ReadOnly="true" />
        <asp:BoundField DataField="DeductionAmount" HeaderText="Deduction Amount" ReadOnly="true" />
        <asp:BoundField DataField="NetSalary" HeaderText="Net Salary" ReadOnly="true" />
    </Columns>
</asp:GridView>







现在进入aspx.cs页面



制作一个这样的方法..




now go in the aspx.cs page

make one method like this..

public void GetEmpDetails()
{
    try
    {
        SqlConnection con = new SqlConnection("Data Source=RUDRA-PC\\SQLEXPRESS;Initial Catalog=Testing Database;Integrated Security=True");
        con.Open();
        string strQuery = "SELECT EmpName,DepartmentName,Salary,DeductionAmount,(Salary-DeductionAmount)AS NetSalary FROM Employee INNER JOIN Department ON Employee.DepId = Department.DepId ";
        SqlCommand cmd = new SqlCommand(strQuery, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        Gridview1.DataSource = dt;
        Gridview1.DataBind();
        con.Close();
    }
    catch (Exception ex)
    {
        ClientScript.RegisterStartupScript(GetType(), "fnCall", "<script language='javascript'>alert('" + ex.Message.ToString() + "');</script>");
    }
}





并在PageLoad中像这样调用此方法



and call this Method in the PageLoad like this

protected void Page_Load(object sender, EventArgs e)
{
    GetEmpDetails();
}





还在aspx.cs页面添加三个命名空间...



also add three namespace in the aspx.cs page...

using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;







希望完全,现在你在网格视图中得到数据......

最好的运气....




Hope Fully, now you get the data in the grid view...
Best Of Luck....


这篇关于在gridview中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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