在DataTable中重新进入Datarow

时间:2014-09-28 10:15:34

标签: c# datagridview dialogresult

大家好我使用VC#2008编写代码,允许用户使用索引DataRows按顺序输入多个dataTable,因为他需要使用yes / no dialogresult。

首先,用户使用文本框和组合框使用函数输入数据,然后dilodresult弹出“你想输入更多记录吗?”如果是,请清除文本框并调用该函数并加上index并在datagridview上显示数据,but it doesnot show any data at the datagridviewand the dialogResult countinue showing without performing anything

这个功能代码:

  public DataTable showout2(int index, bool bl)
    {
        DataTable dtab = new DataTable();
        DataRow row = dtab.NewRow();
        string[] newRow = new string[14];
        try
        {

            DataColumn dc1 = new DataColumn("رقم المتسلسل");
            DataColumn dc2 = new DataColumn("رقم الحساب");
            DataColumn dc3 = new DataColumn("أسم الحساب");
            DataColumn dc4 = new DataColumn("المالك");
            DataColumn dc5 = new DataColumn("تاريخ");
            DataColumn dc6 = new DataColumn("قيمة");
            DataColumn dc7 = new DataColumn("نوع العملة");
            DataColumn dc8 = new DataColumn("البيان");
            DataColumn dc9 = new DataColumn("الدائن");
            DataColumn dc10 = new DataColumn("المدين");
            DataColumn dc11 = new DataColumn(" اجمالي الدائن");
            DataColumn dc12 = new DataColumn("اجمالي المدين");
            DataColumn dc13 = new DataColumn("تفاصيل");
            DataColumn dc14 = new DataColumn("التأكيد");
            dtab.Columns.Add(dc1);
            dtab.Columns.Add(dc2);
            dtab.Columns.Add(dc3);
            dtab.Columns.Add(dc4);
            dtab.Columns.Add(dc5);
            dtab.Columns.Add(dc6);
            dtab.Columns.Add(dc7);
            dtab.Columns.Add(dc8);
            dtab.Columns.Add(dc9);
            dtab.Columns.Add(dc10);
            dtab.Columns.Add(dc11);
            dtab.Columns.Add(dc12);
            dtab.Columns.Add(dc13);
            dtab.Columns.Add(dc14);
            DateTime date = new DateTime();
            date = DateTime.Today;

            row[0] = numb.Text.ToString();
            row[1] = Account_numb.Text.ToString();
            row[2] = account_nam.Text.ToString();
            row[3] = owner.Text;
            row[4] = curency.Text.ToString();
            row[5] = date.ToString();
            row[6] = curncyval.Text;
            row[7] = note.Text;
            row[8] = Depet.Text;
            row[9] = criedt.Text;
            row[10] = Total_depet.Text;
            row[11] = Total_credit.Text;
            row[12] = Details.Text;
            row[13] = "false";
            dtab.Rows.InsertAt(row, index);
            dataGridView1.DataSource = dtab;
            dataGridView1.DataSource = dtab;
            DialogResult dialogResult = MessageBox.Show("تريد أدخال سجل أخر؟", "Some Title", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {

                row[0] = numb.Text.ToString();
                row[1] = Account_numb.Text.ToString();
                row[2] = account_nam.Text.ToString();
                row[3] = owner.Text;
                row[4] = curency.Text.ToString();
                row[5] = date.ToString();
                row[6] = curncyval.Text;
                row[7] = note.Text;
                row[8] = Depet.Text;
                row[9] = criedt.Text;
                row[10] = Total_depet.Text;
                row[11] = Total_credit.Text;
                row[12] = Details.Text;
                row[13] = "false";


                numb.Clear();
                owner.Clear();
                Account_numb.Text = "    ";
                account_nam.Text = "      ";
                curency.Text = "       ";
                curncyval.Clear();
                Total_credit.Clear();
                Total_depet.Clear();
                note.Clear();
                Details.Clear();
                criedt.Clear();
                Depet.Clear();




                return showout2(++index, true);
            }
            else if (dialogResult == DialogResult.No)
            {
                return dtab;
            }
        }

1 个答案:

答案 0 :(得分:0)

我不明白究竟是什么问题,但似乎您正在尝试向datagridview添加行,在添加新行后,您会询问用户"他是否要添加更多内容?"

首先需要在应用程序加载时或在构造函数中初始化datagridview。

  public void initDatagrid()
        {
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "رقم المتسلسل" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "رقم الحساب" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "أسم الحساب" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "المالك" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "تاريخ" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "قيمة" });
            dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "نوع العملة" });
            //...
        } 

然后调用此函数添加行

public void showout2()
        {
            object[] newRow = new object[14];
            try
            {

                newRow[0] = "test";
                newRow[1] = "test";
                newRow[2] = "test";
                newRow[3] = "test";
                newRow[4] = "test";
                newRow[5] = "test";
                newRow[6] = "test";
                //...
                dataGridView1.Rows.Add(newRow);

                if (MessageBox.Show("تريد أدخال سجل أخر؟", "Some Title", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    //clear data here ..
                }

            }
            catch (Exception ex)
            {

            }
        }

EX:

public yourClass()
{
            InitializeComponent();
            initDatagrid();
}

private void AddButton_Click(object sender, EventArgs e)
{
    showout2();
}