如何用asp.net中的字符串数组绑定数据表 [英] how to bind data table with string array in asp.net

查看:59
本文介绍了如何用asp.net中的字符串数组绑定数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net中使用字符串数组绑定数据表?

我的问题是这个...
我有一个表,其中的列具有某些值或为null.
我希望将具有null的那些列设置为不适用",然后绑定到datalist

为此,首先从表中选择记录,然后计算行数,然后应用两个嵌套循环,一个用于行,一个用于列,此后我想将表值保存在数组中,然后检查数组的值是否为空,如果数组值为null,则替换为不适用"

我正在这样做

How to bind data table with string array in asp.net?

My problem is this...
I have a table which columns either have some value or null.
I want those columns which have null to be set to "not applicable" and then bind to datalist

For that firstly select the record from the table,then count the number of rows,then apply two nested loop one for row and one for column,after that i want to save table values in array then check the value of array is null or not,if array value is null then replace by "not applicable"

i m doing that

ad = new SqlDataAdapter("select * from posted_data where 1=1" + query, c.con);
                 dt = new DataTable();
                 ad.Fill(dt);

                 if (dt.Rows.Count > 0)
                 {
                     for (int i = 0; i <= dt.Rows.Count; i++)
                       {
                                    for (int j = 0; j <= dt.Columns.Count; j++)
                                    {
                                        string[] arr=new string[];
                                            //if (dt.Rows[i][j].ToString() == "")
                                            // {
                                            //     str = "NA";
                                            // }
                                            //else
                                            //{
                                            //str=dt.Rows[i][j].ToString();
                                            //}

                                            //DataList1.DataSource = str;
                                            //DataList1.DataBind();
                                   }

                      }
                }


正确吗?




我该怎么办.. ??
请帮助我

thnx in adance


is it right???




how can i do..??
plz help me

thnx in adance

推荐答案

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


public partial class Arraysindatalist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //1 dimensional Array::::
        string[] array = { "Anil","Kumar","Anilkumar","Eresh","Archana" };
        Grid1D.DataSource = array;
        Grid1D.DataBind();
        //2 Dimensional array::::
        string[,] arra1 ={

                    { "John", "21" },

                    { "Smith", "33" },

                    { "Ryder", "15" },

                    { "Jake", "18"},

                    { "Tom","34" }

                 };
        ArrayList arraylist = new ArrayList();
        for (int i = 0; i < 5; i++)
        {
            arraylist.Add(new ListItem(arra1 [i, 0], arra1 [i, 1]));

        }
        DataList1.DataSource = arraylist;
        DataList1.DataBind();

        //Multi Dimensional Array:::::
        string[,] Multiarray = {

                    { "John", "21", "Berlin", "Germany" },

                    { "Smith", "33" ,"London", "UK"},

                    { "Ryder", "15" ,"Sydney", "Australia"},

                    { "Jake", "18", "Tokyo", "Japan"},

                    { "Tom","34" , "Mumbai", "India"}

                 };

        DataTable dt = new DataTable();
        dt.Columns.Add("Name", Type.GetType("System.String"));
        dt.Columns.Add("Age", Type.GetType("System.String"));
        dt.Columns.Add("City", Type.GetType("System.String"));
        dt.Columns.Add("Country", Type.GetType("System.String"));
        for (int i = 0; i < 5; i++)
        {
            dt.Rows.Add();
            dt.Rows[dt.Rows.Count - 1]["Name"] = Multiarray[i, 0];
            dt.Rows[dt.Rows.Count - 1]["Age"] = Multiarray[i, 1];
            dt.Rows[dt.Rows.Count - 1]["City"] = Multiarray[i, 2];
            dt.Rows[dt.Rows.Count - 1]["Country"] = Multiarray[i, 3];   
        }
       DataList1.DataSource = dt;
       DataList1.DataBind();


       
        
    }
}




试试这个也许你会得到的.


问候,

Anilkumar.D




Try this may be u will get.


Regards,

Anilkumar.D


string[,] multiarray = new string[dt.Rows.Count, dt.Columns.Count];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                multiarray[i, j] = dt.Rows[i][j].ToString();


            }

        }
        DataList1.DataSource = dt;
        DataList1.DataBind();



希望这会有所帮助.

问候,

Anilkumar.D



Hope this helps.

Regards,

Anilkumar.D


string[,] multiarray = new string[dt.Rows.Count, dt.Columns.Count];

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                if (dt.Rows.Count <= 0)
                {
                    Label1.Text = "NotApplicable.";
                }
                else
                    multiarray[i, j] = dt.Rows[i][j].ToString();

              

            }

        }

        DataList1.DataSource = dt;
        DataList1.DataBind();



CheckThis,它有效.


问候,

Anilkumar.D



CheckThis,It Works.


Regards,

Anilkumar.D


这篇关于如何用asp.net中的字符串数组绑定数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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