Not getting values in datatable while Updating gridview row - c#

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

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;

FindNodeBiFieldValue returns null

Logics:
  - choose from the data table rows by the condition "expand = true";
  - perform a search for lines;
  - depending on the value of the string field, we find the node in the "tray list".
In the method ExpandCurentNode() in line
TreeListNode node = treeList1.FindNodeByFieldValue("NodeName", name);
we get null.
Question
Why FindNodeByFieldValue returns null?
Description
TreeList fill method DataBinding().
Property PopulateServiceColumns = true.
Connection Code
public void connect()
{
string catBD = #"с:\db\db.01.accdb";
string conBD = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", catBD);
OleDbConnection connection = new OleDbConnection(conBD);
connection.Open();
string query1 = "SELECT * FROM TableTreeViewNoAi_12";
OleDbCommand cmd1 = new OleDbCommand(query1, connection);
dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns["ID"].AutoIncrement = true;
dt.Columns["ID"].AutoIncrementSeed = 44;
dt.Columns["ID"].AutoIncrementStep = 1;
dt.Columns.Add("PrID", typeof(int));
dt.Columns.Add("sorting", typeof(int));
dt.Columns.Add("NodeName", typeof(string));
dt.Columns.Add("Description", typeof(string));
dt.Columns.Add("Comment", typeof(string));
dt.Columns.Add("dateN", typeof(string));
dt.Columns.Add("Cost", typeof(int));
dt.Columns.Add("typeRecord", typeof(string));
dt.Columns.Add("stImageInd", typeof(int));
dt.Columns.Add("expand", typeof(bool));
dt.Columns.Add("focus", typeof(bool));
try
{
adapter = new OleDbDataAdapter(cmd1);
cmdBuilder = new OleDbCommandBuilder(adapter);
adapter.UpdateCommand = cmdBuilder.GetUpdateCommand();
adapter.InsertCommand = cmdBuilder.GetInsertCommand();
}
catch (Exception ex)
{
string s = ex.Message;
throw;
}
adapter.Fill(dt);
dt.RowChanged += new DataRowChangeEventHandler(Row_Changed);
}
public void DataBinding()
{
// Чтобы отобразить поля "KeyFieldName" и "ParentFieldName"
// Свойство - PopulateServiceColumns = true
// Привязка к ключевым полям
treeList1This.KeyFieldName = "ID";
treeList1This.ParentFieldName = "PrID";
treeList1This.DataSource = dt;
// dataGridView1.DataSource = dt;
}
Form code
private void Frm29UC_Load(object sender, EventArgs e)
{
treeList1.BestFitColumns(); // расширить поля
ExpandCurentNode(); // восстановление дерева после открытия
}
public void ExpandCurentNode() // Восстановление состояния после открытия дерева
{
var selectedDt = conectDB.dt.Select("expand = true"); // DataTable.Select
int i = selectedDt.Length;
foreach (var b in selectedDt)
{
string name = b["NodeName"].ToString();
TreeListNode node = treeList1.FindNodeByFieldValue("NodeName", name); // +- string s = node.GetValue("NodeName").ToString();
node.Expand();
ExpandParentNode(node);
}
}
Ensure that TreeList is completely initialized when calling the ExpandCurentNode method. For this, call the TreeList.ForceInitialize method prior your method.

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

Losing data and gridview 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

How to Pass data table from DataGridView to ReportViewer

How to Pass data table from DataGridView ro ReportViewer ?
in a win Form I have a DataGridView control that fills from a SqlDataAdapter , and I added a check box column to it. for "ready to print"(or smt like this)
now I want to create a report with ReportViewr to show only checked rows in my DataGridView.
here is codes of my form1 (Contains datagridview)
private void Fill_DGV_Sefaresh()
{
try
{
MyDB db = new MyDB();//class that contains my SQL Connection
string sql = "SELECT sid, CONVERT(VARCHAR(10), cast(sdate as date), 111)sdate, sdes, sfor, suname, isdone, sqty FROM sefaresh order by sid desc";
SqlDataAdapter sda = new SqlDataAdapter(sql,db.MyConn);
DataSet ds = new DataSet();
BindingSource bs = new BindingSource();
sda.Fill(ds);
bs.DataSource = ds.Tables[0].DefaultView;
bindingNavigator1.BindingSource = bs;
dataGridView1.DataSource = bs;
}
catch { }
}
codes of my form2 (contains ReportViewer Control)
private SqlDataAdapter idata()
{
MyDB db = new MyDB();
string sql = "SELECT * FROM sefaresh";
SqlDataAdapter sda = new SqlDataAdapter(sql, db.MyConn);
return sda;
}
private void SefareshView_Load(object sender, EventArgs e)
{
idata().Fill(this.DS_Sefaresh.sefaresh);//DS_Sefaresh is a dataset item , sefaresh is a Table adapter
this.reportViewer1.RefreshReport();
}
Try something like the following,
ReportDataSource Rds = new ReportDataSource(dataSetName, bs);
reportViewer1.LocalReport.DataSources.Add(Rds);
Where bs is your BindingSource, dataSetName is the name of the dataset in the report, Rds is the datasource for the ReportViewer, and reportViewer1 is the ReportViewerControl.
If you for some reason need to take the data directly from the DataGridView just replace bs with dataGridView1.DataSource.
Segue
private void tbcaixas_Leave(object sender, EventArgs e)
{
Carga();
int caixas = Convert.ToInt32(tbcaixas.Text);
List<int> lista = new List<int>();
for (int i = 1; i <= caixas; i++)
{
DataRow linha = dt.NewRow();
linha["ncaixa"] = "1/" + i;
linha["loja"] = tbLoja.Text.ToUpper();
dt.Rows.Add(linha);
}
}
private void Carga()
{
dt = new DataTable();
dt.Columns.Add("ncaixa", typeof(string));
dt.Columns.Add("artigo", typeof(string));
dt.Columns.Add("tam", typeof(string));
dt.Columns.Add("cor", typeof(string));
dt.Columns.Add("arte", typeof(string));
dt.Columns.Add("qtd", typeof(string));
dt.Columns.Add("und", typeof(string));
dt.Columns.Add("loja", typeof(string));
dgvDados.DataSource = dt;
}
private void btnImprimir_Click(object sender, EventArgs e)
{
DataTable dtImpressao = ((DataTable)dgvDados.DataSource);
var parametro1 = new ReportParameter("data", dtpData.Text);
var parametro2 = new ReportParameter("cliente", tbCliente.Text);
var parametro3 = new ReportParameter("notafiscal", tbNotaFiscal.Text);
var parametro4 = new ReportParameter("pesobruto", tbPesoBruto.Text);
var parametro5 = new ReportParameter("pesoliquidado", tbPesoLiquidado.Text);
var parametro6 = new ReportParameter("pesoliquidado", cbEmpresa.SelectedItem.ToString());
ReportParameter[] paremetros = new ReportParameter[] { parametro1, parametro2, parametro3, parametro4, parametro5 };
ImpressaoHelper.Imprimir(null, "DataSetRelatorios", "br.com.bonor.Core.relatorios.pcp.Romaneio.rdlc", dtImpressao);
}
}
}

Categories

Resources