Click here to Skip to main content
15,892,298 members
Home / Discussions / C#
   

C#

 
QuestionProblem with adding new record to the datagridview with Master/Child relationship Pin
Hardz20-Oct-10 0:47
Hardz20-Oct-10 0:47 
AnswerRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
rah_sin20-Oct-10 1:07
professionalrah_sin20-Oct-10 1:07 
GeneralRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
Hardz20-Oct-10 14:39
Hardz20-Oct-10 14:39 
GeneralRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
Hardz20-Oct-10 15:27
Hardz20-Oct-10 15:27 
AnswerRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
PIEBALDconsult20-Oct-10 3:21
mvePIEBALDconsult20-Oct-10 3:21 
GeneralRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
Hardz20-Oct-10 14:33
Hardz20-Oct-10 14:33 
GeneralRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
PIEBALDconsult21-Oct-10 3:06
mvePIEBALDconsult21-Oct-10 3:06 
AnswerRe: Problem with adding new record to the datagridview with Master/Child relationship Pin
Hardz28-Oct-10 23:56
Hardz28-Oct-10 23:56 
Hi,

Tnx for the reply and suggestions that helps me a lot.

Anyway, i created a solution that solves for my problem in master/detail relationship, but not really the best one. so here is the revised code as shown below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MasterChild
{
public partial class Form1 : Form
{
bool addIndicator = false;
DataSet ds = new DataSet();
int lastNoOfMasterId;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.masterTableAdapter.Fill(this.dsMaster.Master);
detailLoadValue();
lastno();
}
void lastno()
{
bnMoveLastItem.PerformClick();
lastNoOfMasterId = int.Parse(txtMasterId.Text);
}
void detailLoad()
{
try
{
this.detailTableAdapter.Fill(this.dsDetail.Detail, new System.Nullable<int>(((int)(System.Convert.ChangeType(lblMasterId.Text, typeof(int))))));
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
void detailLoadValue()
{
lblMasterId.Text = txtMasterId.Text;
detailLoad();
}
private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
{
detailLoadValue();
}
private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
{
detailLoadValue();
}
private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
{
detailLoadValue();
}
private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
{
detailLoadValue();
}
private void masterBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
ds.EnforceConstraints = false;
this.masterBindingSource.EndEdit();
this.masterTableAdapter.Update(this.dsMaster.Master);

this.detailBindingSource.EndEdit();
this.detailTableAdapter.Update(this.dsDetail.Detail);
ds.EnforceConstraints = true;
addIndicator = false;
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
if (addIndicator)
{
return;
}
else
{
detailLoadValue();
lastNoOfMasterId += 1;
addIndicator = true;
}
}
private void dgvDetail_RowEnter(object sender, DataGridViewCellEventArgs e)
{
if (addIndicator)
{
lblMasterId.Text = lastNoOfMasterId.ToString();
}
else
{
lblMasterId.Text = txtMasterId.Text;
}
}
}
}

So using the integer lastNoOfMasterId that detects the last number of MasterId and automatically incremented if the user press an add button, which is now a value of MasterId at Details datagrid, replacing the -1 default value during add. With this i am not having trouble now on null value of MasterId at details table and adding a new master along with its details is no longer a problem to me.

But i think this kind of code is much more complicated. Is there any simplified one aside from this?

tnx again for the suggestions.

Har
QuestionNeed help Pin
rksreadero19-Oct-10 16:33
rksreadero19-Oct-10 16:33 
AnswerRe: Need help Pin
JF201519-Oct-10 18:15
JF201519-Oct-10 18:15 
AnswerRe: Need help Pin
Abhinav S19-Oct-10 23:18
Abhinav S19-Oct-10 23:18 
GeneralRe: Need help Pin
Nish Nishant20-Oct-10 4:04
sitebuilderNish Nishant20-Oct-10 4:04 
GeneralRe: Need help Pin
Keith Barrow20-Oct-10 7:58
professionalKeith Barrow20-Oct-10 7:58 
AnswerRe: Need help Pin
Ravi Bhavnani20-Oct-10 4:53
professionalRavi Bhavnani20-Oct-10 4:53 
Question{"Restore failed for Server '\\\\.\\pipe\\3F103E6E-3FD4-47\\tsql\\query'. "} Pin
Mohammad Dayyan19-Oct-10 15:25
Mohammad Dayyan19-Oct-10 15:25 
QuestionExecuteNonQuery problem? [modified] Pin
Emmet_Brown19-Oct-10 8:21
Emmet_Brown19-Oct-10 8:21 
AnswerRe: ExecuteNonQuery problem? Pin
Luc Pattyn19-Oct-10 8:33
sitebuilderLuc Pattyn19-Oct-10 8:33 
AnswerRe: ExecuteNonQuery problem? Pin
Maciej Los19-Oct-10 8:42
mveMaciej Los19-Oct-10 8:42 
GeneralRe: ExecuteNonQuery problem? Pin
Emmet_Brown19-Oct-10 8:47
Emmet_Brown19-Oct-10 8:47 
AnswerRe: ExecuteNonQuery problem? Pin
Maciej Los19-Oct-10 9:12
mveMaciej Los19-Oct-10 9:12 
AnswerSOLVED Pin
Emmet_Brown19-Oct-10 11:04
Emmet_Brown19-Oct-10 11:04 
AnswerRe: ExecuteNonQuery problem? Pin
Pete O'Hanlon19-Oct-10 22:04
mvePete O'Hanlon19-Oct-10 22:04 
QuestionAxWebBrowser object seems to be causing memory leak Pin
HalliHaida19-Oct-10 1:05
HalliHaida19-Oct-10 1:05 
AnswerRe: AxWebBrowser object seems to be causing memory leak PinPopular
Luc Pattyn19-Oct-10 1:39
sitebuilderLuc Pattyn19-Oct-10 1:39 
AnswerRe: AxWebBrowser object seems to be causing memory leak Pin
Pete O'Hanlon19-Oct-10 1:48
mvePete O'Hanlon19-Oct-10 1:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.