Visual Studio 2008

References 에서 추가

- Right button Click on References

- Click "Add Reference.."

- Find "CrystalDecisions.Web" in .Net tab

Posted by 노을지기

Gridview의 특정 부분 필드에 적용하려고 하니 CSS가 제대로 작동하지 않았다.

구글한 결과..

The issue is certainly the size of the <input /> text boxes when in edit mode

Add the <EditRowStyle> element to your gridview to give the edit row a CSS class

<asp:GridView ID="GridView1" runat="server">
    ...
    <EditRowStyle CssClass="GridViewEditRow" /> <%-- add this --%>
</asp:GridView>

Now you can control the size of the textboxes with CSS

.GridViewEditRow input[type=text] {width:50px;} /* size textboxes */
.GridViewEditRow select {width:50px;} /* size drop down lists */
 
textbox 타입이 그냥 text 다.ㅡㅡ;;;
Posted by 노을지기

출처: http://csdotnetcode.blogspot.ca/2007/01/how-to-set-multiple-datakeynames-in.html

//To retrieve the values from a DataKeyNames collection,
        //you can use "Key Names" or "index"


        //Get the values with key names
        string productId = GridView1.DataKeys[e.RowIndex].Values["ProductID"].ToString();
        string orderId = GridView1.DataKeys[e.RowIndex].Values["OrderID"].ToString();

        //Or

        //Get the values with index
        string productId = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        string orderId = GridView1.DataKeys[e.RowIndex].Values[1].ToString();

위와 같이 사용하면 된다. 이걸 몰라서 삽질..ㅡㅡ;;

 

Posted by 노을지기

출처: http://csharpdotnetfreak.blogspot.com/2011/12/detailsview-edititemtemplate-dropdown.html

핵심은 control 을 찾아서 데이터를 입력하는 것이다.

아무튼 대단한듯..ㅡㅡ;;;


소스는 아래와 같다.

DropDownList And RadioButtonList In EditItemTemplate Of DetailsView to Edit and Update In Asp.Net.

DropDownList In EditItemTemplate of DetailsView
In This Example i'm going to explain how to put dropdownlist and radiobutton control in EditItemTemplate of DetailsView and Edit or Update Records of DetailsView in asp.net.

For this i have used SqlDataSource To fetch records from database and populate and update detailsview.

We need to write code in DataBound And ItemUpdating Events of Detailsview to get the selected value in dropdownlist from database when edit or update button is pressed.



HTML SOURCE OF ASPX PAGE
<asp:DetailsView ID="DetailsView2" runat="server"
                 AllowPaging="True" DataKeyNames="ID"
                 AutoGenerateRows="False"
                 DataSourceID="SqlDataSource2" Height="50px"
                 Width="125px"
                 onDataBound="DetailsView2_DataBound"
                 onItemUpdating="DetailsView2_ItemUpdating">
<Fields>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Gender">
<ItemTemplate>
<asp:Label ID="lblGender" runat="server" Text='<%#Eval("Sex") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButtonList ID="rbGender" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%#Eval("Status") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlStatus" runat="server">
<asp:ListItem>Single</asp:ListItem>
<asp:ListItem>Married</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID], [Name], [Sex], [Status] FROM [Details]"
UpdateCommand="UPDATE Details SET Name = @Name, Sex = @Sex,
MaritalStatus = @Stauts WHERE (ID = @ID)">
<UpdateParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="Sex" />
<asp:Parameter Name="Stauts" />
<asp:Parameter Name="ID" />
</UpdateParameters>
</asp:SqlDataSource>

Now to pouplate value from database in selected index of dropdown and then set the new selected value in sqldatasource parameter write below mentioned code in DataBound and ItemUpdating Event of DetailsView respectively.

C# CODE
protected void DetailsView2_DataBound(object sender, EventArgs e)
    {
        if (((DetailsView)sender).CurrentMode == DetailsViewMode.Edit)
        {
                DataRowView row = (DataRowView)((DetailsView)sender).DataItem;
                RadioButtonList rblGender = (RadioButtonList)((DetailsView)sender).FindControl("rbGender");
                DropDownList ddlStatus = (DropDownList)((DetailsView)sender).FindControl("ddlStatus");
                rblGender.SelectedValue = row[2].ToString();
                ddlStatus.SelectedValue = row[3].ToString();
        }
    }
    protected void DetailsView2_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        RadioButtonList rblGender = (RadioButtonList)((DetailsView)sender).FindControl("rbGender");
        DropDownList ddlStatus = (DropDownList)((DetailsView)sender).FindControl("ddlStatus");
        SqlDataSource2.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
        SqlDataSource2.UpdateParameters["Stauts"].DefaultValue = ddlStatus.SelectedValue;
    }


Posted by 노을지기

출처: http://www.dotnetspider.com/resources/7644-Bind-DropDownList-from-DataBase-Using-c.aspx

데이터베이스에 직접 연결하여 가져올 수 있지만,

DataSet을 이용하여 가져올 수 있다.

dataapater와 dataset을 생성한 후 데이터를 입력하면 된다.

핵심 코드는 다음과 같다.

DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "FieldName"; // FieldName of Table in DataBase
DropDownList1.DataValueField = "FieldName";
DropDownList1.DataBind();


--- 원본 소스 ---

string s = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

private void bindtoddl()
{
SqlConnection con = new SqlConnection(s);
SqlCommand cmd = new SqlCommand("select FieldName from TableName", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "FieldName"; // FieldName of Table in DataBase
DropDownList1.DataValueField = "FieldName";
DropDownList1.DataBind();

}



Posted by 노을지기