如何使用C#在gridview中显示数据 [英] How to display data in to gridview using C#

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

问题描述

我的设计如下





请假类型dropdownlist1

来自日期textbox1

To Date textbox2

原因textbox3



提交(按钮)



i有一个按钮当我点击那个按钮我必须显示dropdownlist1,textbox1和textbo2,textbox3值到gridview



为什么用c#



我的尝试:



My design as follows


Leave Type dropdownlist1
From Date textbox1
To Date textbox2
Reason textbox3

Submit (button)

i have one button when i click that button i have to display dropdownlist1,textbox1 and textbo2,textbox3 values to gridview

for that how to do using c#

What I have tried:

  My design as follows


   Leave Type      dropdownlist1
   From Date        textbox1
   To Date          textbox2
   Reason           textbox3

     Submit (button)

i have one button when i click that button i  have to display dropdownlist1,textbox1 and textbo2,textbox3 values to gridview

for that how to do using c#

推荐答案

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebInterface.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click"/>

        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>


































using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebInterface
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DropDownList1.Items.Add("MEDICAL");
            TextBox1.Text = "1Jan2000";
            TextBox2.Text = "2Jan2000";
            TextBox3.Text = "FEVER";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ConcurrentDictionary<string, Leave> MyDictionary = new ConcurrentDictionary<string, Leave>();
            Leave l = new Leave();
           
            l.LeaveType = DropDownList1.SelectedValue.Trim();
            l.FromDate = TextBox1.Text.Trim();
            l.ToDate = TextBox2.Text.Trim();
            l.Reason = TextBox3.Text.Trim();
            MyDictionary.TryAdd(l.FromDate,l);
            GridView1.DataSource = MyDictionary.Select(m => new { m.Value.LeaveType,m.Value.FromDate,m.Value.ToDate,m.Value.Reason });
            GridView1.DataBind();



        }

        public class Leave
        {
            public string LeaveType;
            public string FromDate;
            public string ToDate;
            public string Reason;
        }
    }
}


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

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