使用列表数据结构填充GridView所有项目都以单列显示

时间:2022-03-23 08:01:36

I've created a List and filled it with the values from form. On button click I want the values input in the form by the user to be shown in the GridView. I populated the GridView with the List. But I am getting data in single column.

我创建了一个List并用表单中的值填充它。单击按钮时,我希望用户在表单中输入的值显示在GridView中。我用List填充了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;
            }
        }

My aspx gridview:

我的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>  

I want the data to be displayed in tabular form:

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

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

How can I give HEADER NAME to each column while populating data from List? But the Gridview is showing result in single column:

如何在从List中填充数据时为每列提供HEADER NAME?但是Gridview在单列中显示结果:

Item
1709121012
JERRY KOINO
34
MALE
34567
BLOCK STREET
user
GENERAL

2 个解决方案

#1


0  

Can you change your button click event into this ?

你能将按钮点击事件更改为此吗?

    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;
        }
    }

#2


0  

You can convert list of string to column wise like below

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

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;
            }
        }

#1


0  

Can you change your button click event into this ?

你能将按钮点击事件更改为此吗?

    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;
        }
    }

#2


0  

You can convert list of string to column wise like below

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

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;
            }
        }