繁体   English   中英

用列表数据结构填充GridView,所有项目都显示在单列中

[英]populating GridView with List Data Structure all the items are displayed in single column

我创建了一个列表,并用表单中的值填充了它。 在按钮上单击,我希望用户在表单中输入的值显示在GridView中。 我用列表填充了GridView。 但是我正在单列中获取数据。

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                string result = string.Empty;
                Page.Validate();
                if (!Page.IsValid)
                { return; }

                 result = new C_OPDRegistration().Set_OPDReg(inputName.Value,
                                                                inputAge.Value,
                                                                male.Checked ? 1 : 2,
                                                                inputContact.Value,
                                                                inputAddress.Value,
                                                                "user",
                                                                "1",
                                                                DDL_type.SelectedValue 
                                                            );
                if(result!=null)
                {
                    string patient_id = string.Empty;
                    List<string> list = new List<string>(new string[] { result.ToString(),
                                                                        inputName.Value,
                                                                        inputAge.Value,
                                                                        male.Checked ? "MALE" : "FEMALE",
                                                                        inputContact.Value,
                                                                        inputAddress.Value,
                                                                        "user",
                                                                        DDL_type.SelectedItem.ToString()
                                                                        });

                    DisplayOPD(list);
                    return;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        protected void DisplayOPD(List<string> lst)
        {
            try
            {
                if(lst != null)
                {
                    GridView1.DataSource = lst;
                    GridView1.DataBind();
                    div_grid.Visible = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

我的aspx gridview:

<div id="div_grid" runat="server" visible="false">
    <asp:GridView ID="GridView1"
        runat="server"
        AutoGenerateColumns="true"
        Width="100%"
        HeaderStyle-HorizontalAlign="Left"> 
    </asp:GridView>
</div>  

我希望数据以表格形式显示:

ID  NAME    AGE GENDER   CONTACT    ADDRESS         USER    TYPE
1   JERRY   24  MALE     9987       BLOCK STREET    USER    GENERAL

从列表填充数据时,如何为每个列赋予标题名称? 但是Gridview在单列中显示结果:

Item
1709121012
JERRY KOINO
34
MALE
34567
BLOCK STREET
user
GENERAL

您可以将按钮单击事件更改为此吗?

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string result = string.Empty;
            Page.Validate();
            if (!Page.IsValid)
            { return; }

            result = new C_OPDRegistration().Set_OPDReg(inputName.Value,
                                                           inputAge.Value,
                                                           male.Checked ? 1 : 2,
                                                           inputContact.Value,
                                                           inputAddress.Value,
                                                           "user",
                                                           "1",
                                                           DDL_type.SelectedValue
                                                       );
            if (result != null)
            {
                List<dataObject> list = new List<CapronCRM.dataObject>()
                {
                 new dataObject()
                 {
                     InputName =  inputName.Value,
                     InputAge = inputAge.Value,
                     Gender = male.Checked ? "MALE" : "FEMALE",
                     InputContact = inputContact.Value,
                     InputAddress = inputAddress.Value,
                     Type =  "user",
               SelectedItemText = DDL_type.SelectedItem.ToString()
                }
            };

               DisplayOPD(list);
               return;
            }
        }
        catch (Exception ex)
        {
            throw;
        }
    }

您可以将字符串列表转换为列明智,如下所示

protected void DisplayOPD(List<string> lst)
        {
            try
            {
                if (lst != null)
                {
                    GridView1.DataSource = (from arr in lst select new { ID = arr[0], Name = arr[1], Age = arr[2], Gender = arr[3], Contact = arr[4], Address = arr[5], User = arr[6], Type = arr[7] });
                    GridView1.DataBind();
                    div_grid.Visible = true;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM