Losing data and gridview C# - c#

I have a program by VC2008 that send dataTable I sended it using the constractor I solve it help at losing data entered in dataview C# - to datagridview in another Form it work fine but I want to send only the new row I tried to copy using ImportRrow it to another datatable but it gives Form2 Blank.
and I send the data as datatable by method as following:
public DataTable showout2()
{
DataTable dtab = new DataTable();
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("تفاصيل");
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);
DateTime date = new DateTime();
date = DateTime.Now;
// Create an array for the values.
object[] newRow = new object[10];
Set the values of the array.
string s = numb.Text;
newRow[0] = numb.Text;
newRow[1] = Account_numb.Text;
newRow[2] = Account_nam.Text;
newRow[3] = owner.Text;
newRow[4] = curency.Text;
newRow[5] = comboBox1.Text;
newRow[6] = Depet.Text;
newRow[7] = cridet.Text;
newRow[8] = date.ToString();
newRow[9] = note.Text;
declare DataRow and adding it to DataTable
DataRow row;
dtab.BeginLoadData();
// Add the new row to the rows collection.
row = dtab.LoadDataRow(newRow, true);
return dtab;
}
I transfer the data from Form as following
cashagree fm = cashagree(shouwout2());
I copy the datarow in constractor take datatable as prameter as following:
public Cashagree(DataTable d2)
{
dt.ImportRow(d2.Rows[0]);
}
here I assign the datasource and mange the secound datagridview
private void Cashagree_Load_1(object sender, EventArgs e)
{
try
{
for (int i = 0; i != dt.Rows.Count; i++){
label1.Text= dt.Rows[i].ToString();
if (string.IsNullOrEmpty(dt.Rows[i].ToString()))
{
MessageBox.Show("This is empty");
}
}
dataGridView.DataSource = dt;
dataGridView.Columns[3].Visible = false;
dataGridView.Columns[4].Visible = false;
dataGridView.Columns[5].Visible = false;
dataGridView.Columns[6].Visible = false;
dataGridView.Columns[7].Visible = false;
}
catch (Exception w) { MessageBox.Show("Error" + w.StackTrace); }
}
thank you for helping

Related

Why doesn't it show me the values in my DataGrid that I assign in the DataTable?

I initially wrote the headers manually in my DataGrid and now I want to fill the DataGrid from a DataTable. I wanted to do it like this:
void fillingDataGrid()
{
DataTable dt = new DataTable();
DataColumn id = new DataColumn("id", typeof(int));
DataColumn name = new DataColumn("name", typeof(string));
DataColumn ort = new DataColumn("ort", typeof(string));
DataColumn alter = new DataColumn("alter", typeof(string));
DataColumn land = new DataColumn("land", typeof(string));
dt.Columns.Add(id);
dt.Columns.Add(name);
dt.Columns.Add(ort);
dt.Columns.Add(alter);
dt.Columns.Add(land);
DataRow firstrow = dt.NewRow();
firstrow[0] = 1;
firstrow[1] = "Peter";
firstrow[2] = "Berlin";
firstrow[3] = "18";
firstrow[4] = "Germany";
DataRow secondrow = dt.NewRow();
firstrow[0] = 2;
firstrow[1] = "Karl";
firstrow[2] = "Prag";
firstrow[3] = "12";
firstrow[4] = "Tschechien";
dt.Rows.Add(firstrow);
dt.Rows.Add(secondrow);
gridd.ItemsSource = dt.DefaultView;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.fillingDataGrid();
}
The problem, however, is that if I do it this way, it is not output correctly because it looks like this:
Why doesn't it show me all the data, what do I have to change on my DataTable?
Change this line :
gridd.ItemsSource = dt.DefaultView;
To :
gridd.DataContext = dt.DefaultView;

populating a datatable using linq to sql and stored procedure in asp.net

public void populatetransaction()
{
TRANASACTIONHISTRORYDataContext th = new TRANASACTIONHISTRORYDataContext();
DataTable dt = new DataTable();
var query = from dr
in th.TRANSACTIONSP((System.Nullable<long>)Convert.ToInt32(hidreference.Value))
select dr;
dt = (DataTable)query;
}
here is my code in that th.TRANSACTIONSP((System.Nullable<long>)Convert.ToInt32(hidreference.Value))
is the procedure calling how can i do that
public void populatetransaction()
{
TRANASACTIONHISTRORYDataContext th = new TRANASACTIONHISTRORYDataContext();
DataTable dt = new DataTable();
var query= th.TRANSACTIONSP((System.Nullable<long>)Convert.ToInt32(hidreference.Value));
DataColumn dc1 = dt.Columns.Add("BTC", typeof(string));
dc1.AllowDBNull = true;
DataColumn dc2 = dt.Columns.Add("TRANSACTIONDATE", typeof(DateTime));
dc2.AllowDBNull = true;
DataColumn dc3 = dt.Columns.Add("TRANSACTIONSTATUS", typeof(string));
DataRow dw ;
dc3.AllowDBNull = true;
foreach (var c in query)
{
dw=dt.NewRow();
dw["BTC"] = c.BTC;
dw["TRANSACTIONDATE"] = c.TRANSACTIONDATE;
dw["TRANSACTIONSTATUS"] = c.TRANSACTIONSTATUS;
dt.Rows.Add(dw);
}
}
This was what I want

Not getting values in datatable while Updating gridview row

I have 5 rows in gridview in which I am trying to update one row, While updating, I wrote below code:-
protected void GrdProspective1_UpdateCommand(object sender, Obout.Grid.GridRecordEventArgs e)
{
if (Session["grdEdit"] != null)
{
DtCombo = (DataTable)Session["grdEdit"];
}
else
{
BindDatatable();
}
DataRow[] dataProsUpdate = DtCombo.Select("SR_NO = " + e.Record["SR_NO"]);
dataProsUpdate[0]["PARTY_NAME"] = e.Record["PARTY_NAME"];
dataProsUpdate[0]["FLAT_NO"] = e.Record["FLAT_NO"];
dataProsUpdate[0]["LEASE_NUM"] = e.Record["LEASE_NUM"];
dataProsUpdate[0]["ACTION"] = e.Record["ACTION"];
dataProsUpdate[0]["NO_OF_DAYS"] = e.Record["NO_OF_DAYS"];
dataProsUpdate[0]["REMARKS"] = e.Record["REMARKS"];
GrdProspective1.DataSource = DtCombo;
GrdProspective1.DataBind();
AddToViewState("GrdProspective1");
}
But in this, I am getting error as
Index was outside the bounds of the array.
I am confused why datatable is emppty when there are 5 rows in the gridview
My datatable code:-
public void BindDatatable()
{
DataSet ds = new DataSet();
DataRow dr;
DataColumn SR_NO;
DataColumn PARTY_NAME;
DataColumn FLAT_NO;
DataColumn LEASE_NUM;
DataColumn ACTION;
DataColumn NO_OF_DAYS;
DataColumn REMARKS;
DtCombo = new DataTable();
SR_NO = new DataColumn("SR_NO", typeof(Int32));
PARTY_NAME = new DataColumn("PARTY_NAME", typeof(String));
FLAT_NO = new DataColumn("FLAT_NO", typeof(Int32));
LEASE_NUM = new DataColumn("LEASE_NUM", typeof(Int32));
ACTION = new DataColumn("ACTION", typeof(String));
NO_OF_DAYS = new DataColumn("NO_OF_DAYS", typeof(Int32));
REMARKS = new DataColumn("REMARKS", typeof(String));
DtCombo.Columns.Add(SR_NO);
DtCombo.Columns.Add(PARTY_NAME);
DtCombo.Columns.Add(FLAT_NO);
DtCombo.Columns.Add(LEASE_NUM);
DtCombo.Columns.Add(ACTION);
DtCombo.Columns.Add(NO_OF_DAYS);
DtCombo.Columns.Add(REMARKS);
}
Complete solution:
protected void GrdProspective1_UpdateCommand(object sender, Obout.Grid.GridRecordEventArgs e)
{
if (Session["grdEdit"] != null)
{
DtCombo = (DataTable)Session["grdEdit"];
}
else
{
return;
}
DataRow[] dataProsUpdate = DtCombo.Select("SR_NO = " + e.Record["SR_NO"]);
dataProsUpdate[0]["PARTY_NAME"] = e.Record["PARTY_NAME"];
dataProsUpdate[0]["FLAT_NO"] = e.Record["FLAT_NO"];
dataProsUpdate[0]["LEASE_NUM"] = e.Record["LEASE_NUM"];
dataProsUpdate[0]["ACTION"] = e.Record["ACTION"];
dataProsUpdate[0]["NO_OF_DAYS"] = e.Record["NO_OF_DAYS"];
dataProsUpdate[0]["REMARKS"] = e.Record["REMARKS"];
GrdProspective1.DataSource = DtCombo;
GrdProspective1.DataBind();
AddToViewState("GrdProspective1");
}
public void DisplayGridEdit()
{
OracleCommand cmd = new OracleCommand("SELECT...", ObjPriCon);
DtCombo = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(DtCombo);
GrdProspective1.DataSource = DtCombo;
GrdProspective1.DataBind();
AddToViewState("GrdProspective1");
}

losing data entered in dataview C#

I am using Visual C# 2008 to make a application that takes the data from textboxes and displays it in datagridview in another form the conforming make it entered to the database.
I send the data using dataTable with a function entered the data without any symentic error but when I call the other for the datagridview comes empty and the database comes empty. When I duplicate a primary key it gives an error stating "cannot duplicate primary key".
This is the code for the function that transfers that datatable
public DataTable showout() {
DataTable dtab = new DataTable();
DataColumn dc1 = new DataColumn("رقم المتسلسل");
DataColumn dc2 = new DataColumn("رقم الحساب");
DataColumn dc3 = new DataColumn("أسم الحساب");
dtab.Columns.Add(dc1);
dtab.Columns.Add(dc2);
dtab.Columns.Add(dc3);
// Create an array for the values.
object[] newRow = new object[3];
// Set the values of the array.
string s = numb.Text;
newRow[0] =numb.Text;
newRow[1] = textBox5.Text;
newRow[2] =note.Text;
DataRow row;
dtab.BeginLoadData();
// Add the new row to the rows collection.
row = dtab.LoadDataRow(newRow, true);
return dtab;
}
this is the code that I call the function in the other From
private void Cashagree_Load(object sender, EventArgs e) {
dataGridView1.DataSource = ch.showout();
}
the second datagrid entering function its in the same class
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = true;
dataGridView1.DataSource = showout();
entering(true);
}
and this is the entering to the database
public void entering(bool bl)
{try{
if (bl)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DateTime Date = DateTime.Today;
SqlCommand cmd = new SqlCommand("INSERT INTO Accont(Account_ID,Account_Name,Owners,Curency,Curncytype,Depet,Credet_devet,Date,Note) VALUES (#AccountID, #AccountName, #Owner, #Curncy,#Curncytype,#Depet,#Cridetdevet,#Date,#Note)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("#AccountID",numb.Text);
cmd.Parameters.AddWithValue("#AccountName", comboBox1.SelectedText.ToString());
cmd.Parameters.AddWithValue("#Owner", owner.Text);
cmd.Parameters.AddWithValue("#Curncy", curency.Text);
cmd.Parameters.AddWithValue("#Curncytype", curncyval.Text);
cmd.Parameters.AddWithValue("#Depet", Depet.Text);
cmd.Parameters.AddWithValue("#Cridetdevet", textBox5.Text);
cmd.Parameters.AddWithValue("#Date", Date);
cmd.Parameters.AddWithValue("#Note", note.Text);
connection.Open();//Owner
cmd.ExecuteNonQuery();}}
}
catch(Exception ee)
{MessageBox.Show(ee.Message);}
the conforming from the another form
private void button1_Click_1(object sender, EventArgs e)
{
ch.entering(true);
Close();
}
Instead of using the dtab.LoadDataRow, you should be using the dtab.Rows.Add(datarow) method.
An example of how to do this:
public DataTable showout()
{
DataTable dtab = new DataTable();
// More efficient way of adding the columns with types:
dtab.Columns.Add("رقم المتسلسل", typeof(String));
dtab.Columns.Add("رقم الحساب", typeof(String));
dtab.Columns.Add("أسم الحساب", typeof(String));
/*
DataColumn dc1 = new DataColumn("رقم المتسلسل");
DataColumn dc2 = new DataColumn("رقم الحساب");
DataColumn dc3 = new DataColumn("أسم الحساب");
dtab.Columns.Add(dc1);
dtab.Columns.Add(dc2);
dtab.Columns.Add(dc3);
*/
// Create a new row using the .NewRow method
DataRow datRow = dtab.NewRow();
datRow["رقم المتسلسل"] = numb.Text;
datRow["رقم الحساب"] = textBox5.Text;
datRow["أسم الحساب"] = note.Text;
// Add the new row to the DataTable
dtab.Rows.Add(datRow);
return dtab;
}
Reference:
How to: Add Rows to a DataTable
Adding Data to a DataTable
In solve it by the sending the DataTable dt from the first Form cash to the second Form cashagree as a prameter by calling the method that return datagridview
in cash form I wrote this:
cashagree gc2 = cashagree(showout());
in cashagree form I wrote this
DataTable dt = new DstsTsble();
public cashagree(DataTable d2){
dt =d2;
}
and in the load of `cashagree_Load` I asign the datagridview datasoure
private void Cashagree_Load(object sender, EventArgs e)
{
if (dt.Rows[0].IsNull(dt.Columns[0]))
{
MessageBox.Show("There no primary key");
Close();
}
dataGridView1.DataSource = dt;
dataGridView1.Columns[7].Visible = false;// Iwantn't all the datatable so I diapple some Columns
dataGridView1.Columns[5].Visible = false;
dataGridView1.Columns[7].Visible = false;
dataGridView1.Columns[6].Visible = false;
dataGridView1.Columns[4].Visible = false;
}

Data Table Not Binding to Grid View

I am creating a data table and trying to bind it wit a grid view.
Code:
DataTable timeTable = new DataTable();
DataColumn colSubject = new DataColumn("Subject Name");
DataColumn colClass = new DataColumn("Class");
DataColumn colTeacher = new DataColumn("Lecturer");
DataColumn colStartTime = new DataColumn("Start");
DataColumn colEndTime = new DataColumn("End");
colSubject.DataType = System.Type.GetType("System.String");
colClass.DataType = System.Type.GetType("System.String");
colTeacher.DataType = System.Type.GetType("System.String");
colStartTime.DataType = System.Type.GetType("System.String");
colEndTime.DataType = System.Type.GetType("System.String");
timeTable.Columns.Add(colSubject);
timeTable.Columns.Add(colClass);
timeTable.Columns.Add(colTeacher);
timeTable.Columns.Add(colStartTime);
timeTable.Columns.Add(colEndTime);
int numOfRows = 0;
SqlDataReader read = null;
try
{
conn.Open();
read = getTimetable.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
if (numOfRows < 3)
{
DataRow row = timeTable.NewRow();
row[colSubject] = read["subject_name"].ToString();
row[colClass] = read["resource_name_number"].ToString();
row[colTeacher] = read["userDetail_name"].ToString();
row[colStartTime] = read["timeSlot_startTime"].ToString();
row[colEndTime] = read["timeSlot_endTime"].ToString();
timeTable.Rows.Add(row);
numOfRows++;
}
}
gridTimeTable.DataSource = timeTable;
gridTimeTable.DataBind();
}
}
Now The Data table is Created Fine and all cause I used a breakpoint to see whether it was being created the thing is it is not populating the grid view. any ideas?
Thanks :)

Categories

Resources